大家好,我是SwiftUI 的新手。只有在檢查了一個條件后,我才需要展示一個包含兩個帶有模態展示的 textField 的視圖。
示例.. 當用戶按下登錄按鈕時,我需要應用程式檢查資料庫是否存在用戶。如果用戶存在,他可以訪問該應用程式,否則他必須顯示一個視圖,他必須在其中輸入他的姓名和姓氏。
與UIKit我用這個存在一個structure或class
self.present(userFound ? Home() : UserNameInfo(), animated: true, completion: nil)
但SwiftUI我不知道如何解決這個問題。
你能以任何方式幫助我嗎?
我的代碼
var body: some View {
ZStack(alignment: .top) {
Color(.black).ignoresSafeArea()
gradientBackground().ignoresSafeArea()
VStack(alignment: .center, spacing: 25) {
Spacer()
logoStack()
// Messaggio di benvenuto
VStack(alignment: .leading, spacing: 15) {
Text("Effettua l'accesso al tuo account")
.font(.title2.bold())
Text("Riserva un momento esclusivo prenotando un taglio tradizionale oppure una rasatura con panni caldi e trattamenti viso")
.font(.callout.weight(.light))
}
.foregroundColor(.white)
//.dynamicTypeSize(.medium)
// Apple Sign In Button
SignInWithAppleView()
.frame(width: screen.size.width - 56)
.frame(height: 45, alignment: .center)
.onTapGesture(perform: showAppleLoginView)
// Divisore
Divider().background(.gray)
// Messaggio sull'accettazione della Privacy e delle Condizioni di utilizzo
VStack(spacing: 5) {
Text("Continuando dichiaro di accettare le ")
.multilineTextAlignment(.center)
HStack(spacing: 0) {
Button("Condizioni di Utilizzo") {}
.foregroundColor(.white)
.font(.footnote.bold())
Text(" ed i ")
Button("Termini sulla Privacy") {}
.foregroundColor(.white)
.font(.footnote.bold())
Text(".")
}
}
.foregroundColor(.gray)
.font(.footnote)
.padding(.bottom, 25)
}
.padding(.horizontal)
}
private func showAppleLoginView() {
// Show modalview if user not exist in Firebase
}
// MARK: - Apple Sign In Button View Representable
struct SignInWithAppleView: UIViewRepresentable {
typealias UIViewType = ASAuthorizationAppleIDButton
func makeUIView(context: Context) -> UIViewType {
ASAuthorizationAppleIDButton(type: .signIn, style: .white)
}
func updateUIView(_ uiView: UIViewType, context: Context) {}
}
uj5u.com熱心網友回復:
你可以使用修飾符 .fullScreenCover
&@State var當你想顯示模式時,你只需要傳遞一個系結到你設定為 true的系結。
例子
struct ExampleScreenView: View {
@State var showModal: Bool = false
var body: some View {
VStack {
Text("Some Text")
.padding()
Button {
showModal = true
} label: {
Text("Show other view")
}
}
.fullScreenCover(isPresented: $showModal) {
VStack {
Color.white
Text("Some other text")
.padding()
Button {
showModal = false
} label: {
Text("close view")
}
}
}
}
}
這個例子第一個視圖有一個按鈕將布林值設定為true,并顯示模態視圖,模態視圖有一個按鈕將布林值設定為false并關閉視圖。
該按鈕只是為了示例,但您可以使用任何邏輯來設定布林值。為 true,然后您可以在 fullScreenCover 中呈現您選擇的任何視圖。
特定于登錄場景,您可以使用.onReceive修飾符來監聽您的登錄成功代碼發送的通知。
.onReceive(NotificationCenter.default.publisher(for: Notification.Name(rawValue: "didLogin"))
&
NotificationCenter.post(name:Notification.Name("didLogin"), object: nil)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/409030.html
標籤:
下一篇:Xcode13:鎖定視圖方向
