簡體   English   中英

SwiftUI 使用按鈕更改視圖

[英]SwiftUI Change View with Button

我知道有 PresentationButton 和 NavigationButton 可以在最新的 SwiftUI 中更改視圖。 但是我想做一個簡單的操作,如下所示。 當用戶單擊“登錄”按鈕時,如果憑據正確,它將登錄並執行 segue(在這種情況下更改視圖)。 但是,我無法在 PresentationButton 中檢查它們是否正確,也無法更改普通按鈕中的視圖。 有沒有另一種方法可以做到這一點?

  @IBAction func signInClicked(_ sender: Any) {

        if emailText.text != "" && passwordText.text != "" {

            Auth.auth().signIn(withEmail: emailText.text!, password: passwordText.text!) { (userdata, error) in
                if error != nil {
                   //error
                } else {

                   performSegue(withIdentifier: "toFeedActivity", sender: nil)


                }
            }

        } else {
            //error
        }




    }

這是一種方法。

struct AppContentView: View {
    
    @State var signInSuccess = false
    
    var body: some View {
        return Group {
            if signInSuccess {
                AppHome()
            }
            else {
                LoginFormView(signInSuccess: $signInSuccess)
            }
        }
    }
}

struct LoginFormView : View {
    
    @State private var userName: String = ""
    @State private var password: String = ""
    
    @State private var showError = false
    
    @Binding var signInSuccess: Bool
    
    var body: some View {
        VStack {
            HStack {
                Text("User name")
                TextField("type here", text: $userName)
            }.padding()
            
            HStack {
                Text(" Password")
                TextField("type here", text: $password)
                    .textContentType(.password)
            }.padding()
            
            Button(action: {
                // Your auth logic
                if(self.userName == self.password) {
                    self.signInSuccess = true
                }
                else {
                    self.showError = true
                }
                
            }) {
                Text("Sign in")
            }
            
            if showError {
                Text("Incorrect username/password").foregroundColor(Color.red)
            }
        }
    }
}

struct AppHome: View {
    
    var body: some View {
        VStack {
        Text("Hello freaky world!")
        Text("You are signed in.")
        }
    }
}

我在我的一個應用程序中有同樣的需求,我找到了一個解決方案......

基本上你需要在 NavigationView 中插入你的主視圖,然后在你的視圖中添加一個不可見的 NavigationLink,創建一個 @state var 來控制你何時想要推送視圖並在你的登錄回調中更改它的值......

那是代碼:

struct ContentView: View {
    @State var showView = false
    var body: some View {
        NavigationView {
            VStack {
                Button(action: {
                    print("*** Login in progress... ***")
                    DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                        self.showView = true
                    }
                }) {
                    Text("Push me and go on")
                }

                //MARK: - NAVIGATION LINKS
                NavigationLink(destination: PushedView(), isActive: $showView) {
                    EmptyView()
                }
            }
        }
    }
}


struct PushedView: View {
    var body: some View {
        Text("This is your pushed view...")
            .font(.largeTitle)
            .fontWeight(.heavy)
    }
}

嘗試使用 state 和 .sheet

struct ContentView: View {
    @State var showingDetail = false

    var body: some View {
        Button(action: {
            self.showingDetail.toggle()
        }) {
            Text("Show Detail")
        }.sheet(isPresented: $showingDetail) {
            DetailView()
        }
    }
}

您可以使用帶有標簽的導航鏈接,

這是代碼:

首先,聲明標簽var

@State var tag : Int? = nil

然后創建您的按鈕視圖:

Button("Log In", action: {
                        Auth.auth().signIn(withEmail: self.email, password: self.password, completion: { (user, error) in
                            if error == nil {
                                self.tag = 1
                                print("success")
                            }else{
                                print(error!.localizedDescription)
                            }
                        })

因此,當登錄成功標簽將變為 1 並且當標簽變為 1 時,您的導航鏈接將被執行

導航鏈接代碼:

NavigationLink(destination: HomeView(), tag: 1, selection: $tag) {
                EmptyView()
                }.disabled(true)

如果您使用 Form 使用 .disabled 因為這里空視圖將在表單上可見並且您不希望您的用戶單擊它並轉到 homeView。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM