因此,我在正文中創建了一個 Button 視圖,并將其操作設定為 Google 登錄操作,但我希望它在登錄流程完成時也轉換為新視圖。問題是我已將按鈕的標簽設定為導航鏈接,當我單擊它時,它會直接轉換到下一個視圖。我怎樣才能延遲過渡?對于背景關系,VoucherView 是我要轉換到的下一個視圖。
Button {
guard let clientID = FirebaseApp.app()?.options.clientID else { return }
// Create Google Sign In configuration object.
let config = GIDConfiguration(clientID: clientID, serverClientID: GlobalConstants.BACKEND_SERVER_REQUEST_ID_TOKEN)
guard let presenter = CommonHelper.GetRootViewController() else {return}
// Start the sign in flow!
GIDSignIn.sharedInstance.signIn(with: config, presenting: presenter) {user, error in
if let error = error {
print (error.localizedDescription)
return
}
guard
let authentication = user?.authentication,
let idToken = authentication.idToken
else {
print ("Something went wrong!")
return
}
print (authentication)
print (idToken)
let credential = GoogleAuthProvider.credential(withIDToken: idToken,
accessToken: authentication.accessToken)
print (credential)
UserManager.shared.firebaseAuthentication(credential: credential)
signed = true
}
} label: {
NavigationLink {
VoucherView()
} label: {
Text("Sign In")
}
}
編輯:在我嘗試使用 isValid 作為 @State 變數后,在每次登錄和退出回圈后,螢屏都會關閉。
首次登錄
首先退出
二次登錄
第二次退出
uj5u.com熱心網友回復:
而不是使用的NavigationLink 內部你的Button,你可以使用NavigationLink帶有EmptyView一個標簽,然后用激活編程isActive引數。
請參閱以下示例 - 為簡潔起見,Google 登錄程序僅替換為一個簡單的異步閉包。
struct ContentView: View {
@State private var navigateToNextView = false
var body: some View {
NavigationView {
VStack {
Button {
DispatchQueue.main.asyncAfter(deadline: .now() 1) {
navigateToNextView = true
}
} label: {
Text("Sign in")
}
NavigationLink(destination: View2(), isActive: $navigateToNextView) { EmptyView()
}
}
}.navigationTitle("Test")
}
}
struct View2 : View {
var body: some View {
Text("Next view")
}
}
還有一些注意事項:
- 請注意,層次結構中必須有一個
NavigationView(確保它只有一個)才能作業 - 一般來說,異步作業
ObservableObject在 a中比在 a 中完成得更好View。您可以考慮將登錄功能移動到 anObservableObject并在該物件上創建navigateToNextViewa@Published屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/360583.html
上一篇:C#Windows表單-帶有事件的不可見元素(單擊和滑鼠輸入/離開)
下一篇:如何有效地級聯CIFilters
