0

I don't know how to navigate between views with buttons.

The only thing I've found online is detail view, but I don't want a back button in the top left corner. I want two independent views connected via two buttons one on the first and one on the second.

In addition, if I were to delete the button on the second view, I should be stuck there, with the only option to going back to the first view being crashing the app.

In storyboard I would just create a button with the action TouchUpInSide() and point to the preferred view controller.

Also do you think getting into SwiftUI is worth it when you are used to storyboard?

0

1 Answer 1

1

One of the solutions is to have a @Statevariable in the main view. This view will display one of the child views depending on the value of the @Statevariable:

struct ContentView: View {
    @State var showView1 = false

    var body: some View {
        VStack {
            if showView1 {
                SomeView(showView: $showView1)
                    .background(Color.red)
            } else {
                SomeView(showView: $showView1)
                    .background(Color.green)
            }
        }
    }
}

And you pass this variable to its child views where you can modify it:

struct SomeView: View {
    @Binding var showView: Bool

    var body: some View {
        Button(action: {
            self.showView.toggle()
        }) {
            Text("Switch View")
        }
    }
}

If you want to have more than two views you can make @State var showView1 to be an enum instead of a Bool.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.