SwiftUI:如何关闭模态 sheet 然后一起执行导航链接
SwiftUI: How to dismiss a modal sheet and then execute a navigationlink together
我有一个带有欢迎视图的入职流程,其中有两个按钮可以打开用于注册和登录的模式表。
注册完成后,该按钮应关闭注册模式视图并转换到另一个仪表板视图。
如何关闭模式,然后执行指向仪表板视图的导航链接?
import SwiftUI
import Firebase
struct Signup: View {
@State private var isAuthCompleted: Bool = false
@State private var isShowingAlert = false
@State private var localMsg: String = ""
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
.
.
.
// signup button
NavigationView {
VStack {
NavigationLink(destination: ContentView(),
isActive: self.$isAuthCompleted) {
Text("")
}
Button(action: {
Auth.auth().createUser(
withEmail: self.email,
password: self.password
) {
authResult, error in
if let e = error {
print(e.localizedDescription)
self.localMsg = e.localizedDescription
self.isShowingAlert = true
} else {
self.presentationMode.wrappedValue.dismiss() // modal sheet dismiss
self.isAuthCompleted = true // navigationlink execution
}
}
}) {
HStack {
Spacer()
Text("Sign Up")
.font(.headline)
.foregroundColor(.white)
Spacer()
}
.padding()
.background(Color.green)
.cornerRadius(20.0)
}
.alert(isPresented: $isShowingAlert) {
Alert(title: Text("Wait A Minute"), message: Text(self.localMsg), dismissButton: .default(Text("Got it!")))
}
}
.navigationBarTitle("")
.navigationBarHidden(true)
}
您可以使用该方法显示模态并在关闭时执行操作。它看起来应该类似于:
import SwiftUI
struct Signup: View {
// Property to keep track of your modal state
@State var isPresented = false
var body: some View {
NavigationView {
VStack {
Button(action: {
// Show / hide the modal view with toggle()
self.isPresented.toggle()
}, label: {
// Your button label
})
.sheet(isPresented: $isPresented,
onDismiss: {
// What happen when your modal dismiss
}) {
// Modal view that opens to be declared here
}
}
}
}
}
我有一个带有欢迎视图的入职流程,其中有两个按钮可以打开用于注册和登录的模式表。
注册完成后,该按钮应关闭注册模式视图并转换到另一个仪表板视图。
如何关闭模式,然后执行指向仪表板视图的导航链接?
import SwiftUI
import Firebase
struct Signup: View {
@State private var isAuthCompleted: Bool = false
@State private var isShowingAlert = false
@State private var localMsg: String = ""
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
.
.
.
// signup button
NavigationView {
VStack {
NavigationLink(destination: ContentView(),
isActive: self.$isAuthCompleted) {
Text("")
}
Button(action: {
Auth.auth().createUser(
withEmail: self.email,
password: self.password
) {
authResult, error in
if let e = error {
print(e.localizedDescription)
self.localMsg = e.localizedDescription
self.isShowingAlert = true
} else {
self.presentationMode.wrappedValue.dismiss() // modal sheet dismiss
self.isAuthCompleted = true // navigationlink execution
}
}
}) {
HStack {
Spacer()
Text("Sign Up")
.font(.headline)
.foregroundColor(.white)
Spacer()
}
.padding()
.background(Color.green)
.cornerRadius(20.0)
}
.alert(isPresented: $isShowingAlert) {
Alert(title: Text("Wait A Minute"), message: Text(self.localMsg), dismissButton: .default(Text("Got it!")))
}
}
.navigationBarTitle("")
.navigationBarHidden(true)
}
您可以使用该方法显示模态并在关闭时执行操作。它看起来应该类似于:
import SwiftUI
struct Signup: View {
// Property to keep track of your modal state
@State var isPresented = false
var body: some View {
NavigationView {
VStack {
Button(action: {
// Show / hide the modal view with toggle()
self.isPresented.toggle()
}, label: {
// Your button label
})
.sheet(isPresented: $isPresented,
onDismiss: {
// What happen when your modal dismiss
}) {
// Modal view that opens to be declared here
}
}
}
}
}