如何在swiftui中转换到新视图

0yg35tkg  于 2023-02-21  发布在  Swift
关注(0)|答案(1)|浏览(124)

我的目标是让用户点击一个按钮,然后让他们选择是或取消,这工作正常。如果选择是,它应该移动到相机视图。我得到错误:未使用"NavigationLink〈Label,Destination〉"初始值设定项的结果。

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)
            }
        }
    }
}
}
a1o7rhls

a1o7rhls1#

要使用按钮进行导航,我们需要使用一个变量作为触发器,将按钮 Package 在NavigationLink中并将相关变量更新为适当的值将触发导航。
下面是更新后的ContentViewCameraView保持不变。

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
                }
                
            }
        }
    }
}

相关问题