如何在SwiftUI中切换到新视图。

huangapple go评论64阅读模式
英文:

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实体引号(&quot;)替换为常规引号以使代码有效。如果您的代码中确实使用了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(&quot;+&quot;)
                    }
                        .frame(width: 40, height: 40)
                        .symbolVariant(.fill)
                        .background(.red)
                        .cornerRadius(15)
                        .foregroundColor(.white)
                        .padding(.trailing,300)
                    Spacer() 
            }
            .alert(&quot;Create Event Here?&quot;, isPresented: $showAlert) {
                Button(&quot;Yes&quot;){NavigationLink(&quot;addCameraView&quot;, destination: CameraView())//*****gets the error
                }
                Button(&quot;Cancel&quot;, 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: &quot;snow&quot;)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .clipShape(Circle())
                        .frame(width: 300, height: 300)
                }
                Button(&quot;Camera&quot;) {
                    self.sourceType = .camera
                    self.imagePickerDisplay.toggle()
                }.padding()
            }
            .navigationBarTitle(&quot;Take a Photo of the Event&quot;)
            .sheet(isPresented: self.$imagePickerDisplay) {
                ImagePickerView(selectedImage: self.$selectedImage, sourceType: self.sourceType)
            }
        }
    }
}
}

答案1

得分: 1

为了使用按钮进行导航,我们需要使用一个变量作为触发器。将按钮包装在 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("在这里创建事件?", 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(&quot;+&quot;)
                }
                .frame(width: 40, height: 40)
                .symbolVariant(.fill)
                .background(.red)
                .cornerRadius(15)
                .foregroundColor(.white)
                .padding(.trailing,300)
                Spacer()
            }
            .alert(&quot;Create Event Here?&quot;, isPresented: $showAlert) {
                NavigationLink(destination: CameraView(), tag: 1, selection: $selection) {
                    Button(&quot;Yes&quot;){
                        selection = 1
                    }
                }
                Button(&quot;Cancel&quot;, role: .cancel) {
                    selection = nil
                }
                
            }
        }
    }
}

huangapple
  • 本文由 发表于 2023年2月19日 05:11:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定