【问题标题】:Present a new view when a condition has been met in SwiftUI在 SwiftUI 中满足条件时显示新视图
【发布时间】:2022-01-11 19:12:41
【问题描述】:

大家好,我是 SwiftUI 的新手。只有在检查了一个条件后,我才需要展示一个包含两个文本字段的视图。


示例 .. 当用户按下登录按钮时,我需要应用程序检查数据库中是否存在用户。如果用户存在,他可以访问该应用程序,否则他必须显示一个必须输入他的姓名和姓氏的视图。

对于UIKit,我用它来展示structureclass

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) {}
}

【问题讨论】:

    标签: ios class if-statement swiftui presentmodalviewcontroller


    【解决方案1】:

    你可以使用修饰符.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")
                    }
                }
            }
        }
    }
    

    这个例子第一个视图有一个按钮,将 bool 设置为 true,并显示模态,模态视图有一个按钮,将 bool 设置为 false 并关闭视图。

    按钮只是为了示例,但您可以使用任何逻辑来设置布尔值。为 true,然后您可以在 fullScreenCover 中呈现您选择的任何视图。

    特定于登录场景,您可以使用.onReceive 修饰符来监听您的登录成功代码发送的通知。

    .onReceive(NotificationCenter.default.publisher(for: Notification.Name(rawValue: "didLogin"))
    

    &

    NotificationCenter.post(name:Notification.Name("didLogin"), object: nil)
    

    【讨论】:

    • 是的,我已经看到了……我的问题是必须在不使用按钮的情况下呈现模态视图,因为我需要在考虑条件后才将新视图呈现给用户……示例 .. 如果用户存在于数据库中,则不显示任何视图,否则如果用户不存在于数据库中,我必须显示新视图。所有这些仅在用户登录 Firebase 后才会发生。在用户登录到 Firebase 后立即检查用户是否存在。
    • 我正在使用当前代码编辑我的帖子
    • 一旦满足条件,将 bool 设置为 true,这就是绑定的目的。
    • 如果登录在应用程序的其他位置,您可以使用 .onReceive(Notification) 来监听登录通知。
    猜你喜欢
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多