英文:
How to transition to a new view in swiftui
问题
我的目标是让用户点击一个按钮,然后让他们选择是或取消,这部分可以正常工作。如果选择是,应该转到相机视图。我收到了错误消息:“NavigationLink<Label, Destination>”的初始化程序未被使用。
```swift
struct ContentView: View {
@State private var showAlert = false
var body: some View {
NavigationStack {
VStack {
Button {
showAlert = true
} label: {
Text("+")
}
.frame(width: 40, height: 40)
.symbolVariant(.fill)
.background(.red)
.cornerRadius(15)
.foregroundColor(.white)
.padding(.trailing, 300)
Spacer()
}
.alert("创建事件?", isPresented: $showAlert) {
Button("是") {
NavigationLink("addCameraView", destination: CameraView()) // *****出现错误
}
Button("取消", role: .cancel) { }
}
}
}
}
struct CameraView: View {
@State private var sourceType: UIImagePickerController.SourceType = .photoLibrary
@State private var selectedImage: UIImage?
@State private var imagePickerDisplay = false
var body: some View {
NavigationView {
VStack {
if selectedImage != nil {
Image(uiImage: selectedImage!)
.resizable()
.aspectRatio(contentMode: .fit)
.clipShape(Circle())
.frame(width: 300, height: 300)
} else {
Image(systemName: "snow")
.resizable()
.aspectRatio(contentMode: .fit)
.clipShape(Circle())
.frame(width: 300, height: 300)
}
Button("相机") {
self.sourceType = .camera
self.imagePickerDisplay.toggle()
}.padding()
}
.navigationBarTitle("拍摄事件照片")
.sheet(isPresented: self.$imagePickerDisplay) {
ImagePickerView(selectedImage: self.$selectedImage, sourceType: self.sourceType)
}
}
}
}
请注意,我已将您提供的代码中的HTML实体引号("
)替换为常规引号以使代码有效。如果您的代码中确实使用了HTML实体引号,您可以将其还原以满足您的需求。
英文:
My goal is to have the user click a button that then gives them a choice of yes or cancel, this works fine. Is yes is selected it should move to the Camera View. I am getting the error: Result of 'NavigationLink<Label, Destination>' initializer is unused.
struct ContentView: View {
@State private var showAlert = false
var body: some View {
NavigationStack
{
VStack {
Button{
showAlert = true
} label: {
Text("+")
}
.frame(width: 40, height: 40)
.symbolVariant(.fill)
.background(.red)
.cornerRadius(15)
.foregroundColor(.white)
.padding(.trailing,300)
Spacer()
}
.alert("Create Event Here?", isPresented: $showAlert) {
Button("Yes"){NavigationLink("addCameraView", destination: CameraView())//*****gets the error
}
Button("Cancel", role: .cancel) { }
}
}
struct CameraView: View{
@State private var sourceType: UIImagePickerController.SourceType = .photoLibrary
@State private var selectedImage: UIImage?
@State private var imagePickerDisplay = false
var body: some View {
NavigationView {
VStack {
if selectedImage != nil {
Image(uiImage: selectedImage!)
.resizable()
.aspectRatio(contentMode: .fit)
.clipShape(Circle())
.frame(width: 300, height: 300)
} else {
Image(systemName: "snow")
.resizable()
.aspectRatio(contentMode: .fit)
.clipShape(Circle())
.frame(width: 300, height: 300)
}
Button("Camera") {
self.sourceType = .camera
self.imagePickerDisplay.toggle()
}.padding()
}
.navigationBarTitle("Take a Photo of the Event")
.sheet(isPresented: self.$imagePickerDisplay) {
ImagePickerView(selectedImage: self.$selectedImage, sourceType: self.sourceType)
}
}
}
}
}
答案1
得分: 1
为了使用按钮进行导航,我们需要使用一个变量作为触发器。将按钮包装在 NavigationLink
中,并将相关的变量更新为适当的值,将触发导航。
下面是更新后的 ContentView
。CameraView
保持不变。
import SwiftUI
struct ContentView: View {
@State private var showAlert = false
@State var selection: Int? = nil
var body: some View {
NavigationStack
{
VStack {
Button{
showAlert = true
} label: {
Text("+")
}
.frame(width: 40, height: 40)
.symbolVariant(.fill)
.background(.red)
.cornerRadius(15)
.foregroundColor(.white)
.padding(.trailing,300)
Spacer()
}
.alert("在这里创建事件?", isPresented: $showAlert) {
NavigationLink(destination: CameraView(), tag: 1, selection: $selection) {
Button("是"){
selection = 1
}
}
Button("取消", role: .cancel) {
selection = nil
}
}
}
}
}
英文:
To navigate with a button, we need to utilize a variable as our trigger. Wrapping the button in a NavigationLink
and updating the associated variable to the appropriate value will trigger the navigation.
Below you will find the updated ContentView
. CameraView
remains unchanged.
import SwiftUI
struct ContentView: View {
@State private var showAlert = false
@State var selection: Int? = nil
var body: some View {
NavigationStack
{
VStack {
Button{
showAlert = true
} label: {
Text("+")
}
.frame(width: 40, height: 40)
.symbolVariant(.fill)
.background(.red)
.cornerRadius(15)
.foregroundColor(.white)
.padding(.trailing,300)
Spacer()
}
.alert("Create Event Here?", isPresented: $showAlert) {
NavigationLink(destination: CameraView(), tag: 1, selection: $selection) {
Button("Yes"){
selection = 1
}
}
Button("Cancel", role: .cancel) {
selection = nil
}
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论