What's an idiomatic way to switch screens in SwiftUI?

Hi,

In AppKit, there's the famous NSViewController that can be used to display the whole content of a window. One can easily switch the contents of a window there, by simply swapping the view controller with another one.

How can that be achieved in SwiftUI?

I know one way, but it doesn't seem idiomatic at all, something like:

struct MyView : View {
    @State var setupIsDone =  false
    var body: some View {
            if setupIsDone {
                    FirstView()
             } else {
                    SecondPage()
            }
     }
}

So this is really tedious, it will turn into a switch statement and you'd have to pass down the the state variable to each subview in order for the subview to change the screen, like a state machine, only badly implemented.

This forum is only about the Swift language. Apple has requested that questions/comments about their proprietary frameworks should be asked on the Apple developer forums.

2 Likes

Thanks Karl,
Should I simply remove the question?

You can, or you can leave it. But please ask your question to the appropriate forum.

Thanks