简体   繁体   English

在 SwiftUI 完成后更改视图

[英]Change view on completion in SwiftUI

I have developed a view that manages three subviews: login , register and confirm code .我开发了一个管理三个子视图的视图: loginregisterconfirm code

Now I'm developing a separate view that manages all the subviews after login successful.现在我正在开发一个单独的视图,在登录成功后管理所有子视图。 The idea is that after login with success the new is pushed and the old is popped.这个想法是在成功登录后推送新的并弹出旧的。

I've tried with NavigationLink in the login subview but it seems that it doesn't like the isActive change in the rest response (ie in the completion).我已经尝试在登录子视图中使用NavigationLink ,但它似乎不喜欢 rest 响应(即完成时)中的isActive更改。

Here is the code:这是代码:

NavigationLink(
   destination: LoggedView(),
   isActive: self.$pushToLogged,
   label: {
                
   }
).hidden()
func login() {
    let lF = LoginServiceFacade()
    lF.loginMember(mobileNumber: mobileNumber, password: password, completion: {
        (response: LoginResponse?, error: ConnectionErrors?) in
            self.pushToLogged = true
    })
}

This is quite a simple and understandable example of a possible way of doing so:这是一个非常简单易懂的例子,说明了一种可能的做法:

First provide a wrapping view that checks if you are logged in or not.首先提供一个包装视图,用于检查您是否已登录。 Display the according view:显示相应的视图:

struct ContentView: View {
    
    @State var isLoggedIn: Bool = false
    
    var body: some View {
        Group() {
            if self.isLoggedIn {
                Home(isLoggedIn: self.$isLoggedIn)
            } else {
                Login(isLoggedIn: self.$isLoggedIn)
            }
        }
    }
}

Then have your home view which can have whatever structure you want.然后拥有您的主视图,它可以具有您想要的任何结构。 I for eg choose a TabView:例如,我选择了一个 TabView:

struct Home: View {
    
    @Binding var isLoggedIn: Bool
    
    var body: some View {
        TabView {
            Example(subTitle: "First")
                .tabItem {
                    Image(systemName: "list.dash")
                    Text("First")
                }

            Example(subTitle: "Second")
                .tabItem {
                    Image(systemName: "square.and.pencil")
                    Text("Second")
                }
        }
    }
}

struct Example: View {
    
    @State var subTitle: String
    
    var body: some View {
        Text("Hello World \(self.subTitle)")
    }
}

The login view with the completion:完成后的登录视图:

struct Login: View {
    
    @Binding var isLoggedIn: Bool
    
    var myLogin: myLoginClass = myLoginClass()
    
    var body: some View {
        Button(action: {
            self.myLogin.loginWithCompletion { isSucceeded in
                if isSucceeded {
                    self.isLoggedIn.toggle()
                }
            }
        }) {
            Text("Log me in")
        }
    }
}

class myLoginClass {
    // Completing method
    // Here would be your code that would "complete" true if successfully logged in or false if error

    // I'm just waiting to "simulate" the visual process
    func loginWithCompletion(completion: @escaping (Bool) -> Void) {
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            completion(true)
        }
    }
    
}

This produces this example:这产生了这个例子:

示例为 gif

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

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