简体   繁体   English

如何使用 swiftUI MacOS 切换视图?

[英]How do I switch views with swiftUI MacOS?

I found this question - What is the best way to switch views in SwiftUI?我发现了这个问题 -在 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.它不预览。

Ok, here are similar mistakes in your views.好的,您的观点中有类似的错误。 To understand them better to look at View protocol:要更好地了解它们,请View协议:

@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 .所以, body只是一个计算变量,它应该返回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:您的view5x错误是您将 2 个不同的视图放入其中,而不是 1。这里的解决方案是将它们嵌入到VStack ,例如:

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. view4x的问题是类似的 - 我认为,由于if...else语句,不清楚什么视图返回正文。 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.另一种方法是说如果将它们中的每一个都包装到AnyView并在之前键入return ,则主体应该返回什么视图。 In this example changes of goView don't switch views, but you can see the other syntax:在这个示例中, goView更改不会切换视图,但您可以看到其他语法:

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")
            })
        }


    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM