简体   繁体   English

在 swift/swiftui 成功登录时呈现新视图

[英]Render new view on successful log in swift/swiftui

I am working on an ios app and i have had some trouble changing views when the user enters the correct username and password.我正在开发一个 ios 应用程序,当用户输入正确的用户名和密码时,我在更改视图时遇到了一些麻烦。 I have built text fields for username and password and a button that sends the data to the api and checks if its correct.我为用户名和密码构建了文本字段,以及一个将数据发送到 api 并检查其是否正确的按钮。 However, If the user enters correct credentials, I want the view to change to show the homepage.但是,如果用户输入正确的凭据,我希望视图更改以显示主页。 What is a good way to do this in swiftui?在 swiftui 中执行此操作的好方法是什么?

You could use NavigationView and NavigationLink if you want to navigate with buttons.如果要使用按钮导航,可以使用NavigationViewNavigationLink For your use case, I would use if-statements.对于您的用例,我会使用 if 语句。 Here an example:这里有一个例子:

struct TestView: View {
    @State private var showingHomeView = false
    var body: some View {
        // If the second view isn't displayed, show the first view.
        if !showingHomeView {
            Button("Go to home") {
                showingHomeView = true
            }
        } else {
            // The home view should be displayed.
            Text("Home")
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM