SwiftUi:通过按钮警报确认导航到辅助页面

时间:2020-10-12 13:35:41

标签: swiftui swiftui-navigationlink

我正在尝试找出如何通过按钮警报导航到辅助屏幕。首先,用户通过主屏幕中的文本字段输入一些字段。

在该屏幕的底部,用户按下“提交”按钮,然后弹出一个警报,询问他们是希望进入辅助屏幕还是取消不采取操作。然后,已通过主屏幕输入的所有字段都将传递到辅助屏幕。然后,如果需要,用户可以在辅助屏幕中选择一次导航回到主屏幕。

这是我尝试过的方法:

struct View1: View {
    @State var txtField1 : String = ""
    @State var txtField2: String = ""
    @State var txtField3: String = ""
    @State var txtField4: String = ""
    @State var txtField5 : String = ""
   
    @State private var showingAlert = false
    @State private var showingView = false

    var body: some View {
    HStack{
                    Button(action: {
                        self.showingAlert = true
                    }) {
                        Text("Submit")
                            .alert(isPresented:$showingAlert) {
                                Alert(title: Text("Would you like to go to second screen?"), message: Text("The second screen will pass all data from the first screen."), primaryButton:.destructive(Text("Continue")){
                                    self.showingView = true
                                    }, secondaryButton: .cancel(Text("Cancel")))
                        }
                    }
                }.popover(isPresented: $showingView){
                    NavigationView{
                        View2(txtField1: self.$txtField1, txtField2: self.$txtField2, txtField3: self.$txtField4, txtField5: self.$txtField5)
                      
                    }

}

使用上面的代码时,它确实导航到我的辅助屏幕(View2),但是它就像一张纸。 View2没有回到View1的导航属性,这就是我要实现的目标。非常感谢您提供任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

要获取Back按钮,您需要一个NavigationLink与一个popover。您可以“隐藏” NavigationLink旁边的Button

import SwiftUI

struct ConfirmNavView: View {
    @State var txtField1 : String = ""
    @State var txtField2: String = ""
    @State var txtField3: String = ""
    @State var txtField4: String = ""
    @State var txtField5 : String = ""
    
    @State private var showingAlert = false
    @State private var showingView = false
    
    var body: some View {
        NavigationView{
            HStack{
                Button(action: {
                    self.showingAlert = true
                }) {
                    Text("Submit")
                        .alert(isPresented:$showingAlert) {
                            Alert(title: Text("Would you like to go to second screen?"), message: Text("The second screen will pass all data from the first screen."), primaryButton:.destructive(Text("Continue")){
                                self.showingView = true
                            }, secondaryButton: .cancel(Text("Cancel")))
                        }
                }
                
                NavigationLink("View2", destination: View2(txtField1: self.$txtField1,
                                                           txtField2: self.$txtField2,
                                                           txtField3: self.$txtField3,
                                                           txtField4: self.$txtField4,
                                                           txtField5: self.$txtField5), isActive: $showingView).hidden().frame(width: 0, height: 0)
            }
        }
        
    }
}
struct View2: View {
    @Binding var txtField1 : String
    @Binding var txtField2: String
    @Binding var txtField3: String
    @Binding var txtField4: String
    @Binding var txtField5 : String
    var body: some View {
        VStack{
            TextField("txtField1", text: $txtField1)
            TextField("txtField2", text:$txtField2)
            TextField("txtField3", text:$txtField3)
            TextField("txtField4", text:$txtField4)
            TextField("txtField5", text:$txtField5)
        }
    }
}
struct ConfirmNavView_Previews: PreviewProvider {
    static var previews: some View {
        ConfirmNavView()
    }
}