SwiftUi:通过按钮警报确认导航到辅助页面
SwiftUi: Navigating to a secondary page through Button Alert Confirmation
我正在尝试弄清楚如何通过按钮警报导航到我的辅助屏幕。首先,用户通过主屏幕中的文本字段输入一些字段。
在这个屏幕的底部,用户按下提交按钮,然后弹出一个警告,询问他们是想被带到辅助屏幕还是取消不被带到。通过主屏幕输入的所有字段都会传递到辅助屏幕。用户随后可以选择在必要时在辅助屏幕中导航回主屏幕。
这是我试过的方法:
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),但它就像 sheet。 View2 没有返回到 View1 的导航属性,这就是我想要实现的目标。非常感谢您对此事的任何帮助,谢谢!
要获得 Back
按钮,您需要 NavigationLink
而不是 popover
。您只需“隐藏”Button
旁边的 NavigationLink
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()
}
}
我正在尝试弄清楚如何通过按钮警报导航到我的辅助屏幕。首先,用户通过主屏幕中的文本字段输入一些字段。
在这个屏幕的底部,用户按下提交按钮,然后弹出一个警告,询问他们是想被带到辅助屏幕还是取消不被带到。通过主屏幕输入的所有字段都会传递到辅助屏幕。用户随后可以选择在必要时在辅助屏幕中导航回主屏幕。
这是我试过的方法:
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),但它就像 sheet。 View2 没有返回到 View1 的导航属性,这就是我想要实现的目标。非常感谢您对此事的任何帮助,谢谢!
要获得 Back
按钮,您需要 NavigationLink
而不是 popover
。您只需“隐藏”Button
NavigationLink
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()
}
}