SwiftUI NavigationLink: Navigate to a destination view AFTER running account creation code successfully

后端 未结 3 1853
渐次进展
渐次进展 2021-01-04 17:30

I want to run account creation logic and then, if successful, transition to the destination view. Otherwise, I\'ll present an error sheet. NavigationLink transitions immedia

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-04 17:47

    Building off of Mohit's answer, making a little more Swifty with an enum that encapsulates screen state:

    
    enum ActionState: Int {
        case setup = 0
        case readyForPush = 1
    }
    
    struct SwiftView: View {
        @State private var actionState: ActionState? = .setup
    
        var body: some View {
    
            NavigationView {
                    VStack {
                        NavigationLink(destination: SomeView, tag: .readyForPush, selection: $actionState) {
                            EmptyView()
                        }
    
    
                        Text("Create Account")
                            .onTapGesture {
                                self.asyncTask()
                        }
                    }
                }
           }
    
    func asyncTask() {
        //some task which on completion will set the value of actionState
        self.actionState = .readyForPush
    }
    

提交回复
热议问题