1

I found this question - What is the best way to switch views in SwiftUI? - but I have not been able to get the answer to work for me.

struct view4x: View {

    @State var goView: Bool = false

    var body: some View {
        if goView {
            view5x(goView1: self.$goView)
        } else {
            Form {
                /* ... */
            }
        }
    }
}

and the button is inside the form:

Button(action: {
     self.goView.toggle()
}) {
     Text("Catalog")
}

and for my other view I have:

struct view5x: View {

    @Binding var goView1: Bool

    var body: some View {
        Text("TEST")
        Button(action: {
            self.goView1.toggle()
        }) {
            Text("Return")
        }
    }
}

I just get errors that both bodies declare an opaque return type. It does not preview.

1 Answer 1

1

Ok, here are similar mistakes in your views. To understand them better to look at View protocol:

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public protocol View {

    /// The type of view representing the body of this view.
    ///
    /// When you create a custom view, Swift infers this type from your
    /// implementation of the required `body` property.
    associatedtype Body : View

    /// Declares the content and behavior of this view.
    var body: Self.Body { get }
}

so, body is just a computed variable and it should return some View. Mistake in your view5x is that you put into it 2 different views instead 1. The solution here is to embed them into VStack for example:

struct view5x: View{

    @Binding var goView1: Bool

    var body: some View{

        VStack {
            Text("TEST")
            Button(action: {
                self.goView1.toggle()
            }) {
                Text("Return")
            }
        }

    }
}

The problem it the view4x is similar - it's unclear what view returns body because of if...else statements, I think. You can fix it in the same way:

struct view4x: View {

    @State var goView: Bool = false

    var body: some View {

        VStack {

            if goView {
                view5x(goView1: $goView)
            } else {
                Button(action: {
                    self.goView.toggle()
                }) {
                    Text("Catalog")
                }
            }
        }


    }

}

The other way is to say what view should body return if you wrap each of them into AnyView and type return before. In this example changes of goView don't switch views, but you can see the other syntax:

struct view4x: View {

    @State var goView: Bool = false

    var body: some View {

        if goView {
            return AnyView(view5x(goView1: $goView))
        } else {
            return AnyView(Button(action: {
                self.goView.toggle()
            }) {
                Text("Catalog")
            })
        }


    }

}
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.