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

后端 未结 3 1794
渐次进展
渐次进展 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:40

    You can wrap your Destination View in a lazy view to prevent the immediate invocation. Here's an example:

    struct LazyView: View {
        let build: () -> Content
        init(_ build: @autoclosure @escaping () -> Content) {
            self.build = build
        }
        var body: Content {
            build()
        }
    }
    

    Eventually, invoke it like this:

    NavigationLink(destination: LazyView(Text("Detail Screen"))){//your code}
    

    I've written a full blog post covering this a few other pitfalls of NavigationLinks in SwiftUI. Refer here.

提交回复
热议问题