【问题标题】:How do I switch views with swiftUI MacOS?如何使用 swiftUI MacOS 切换视图?
【发布时间】:2020-01-05 15:30:14
【问题描述】:

我发现了这个问题 - What is the best way to switch views in SwiftUI? - 但我无法找到适合我的答案。

struct view4x: View {

    @State var goView: Bool = false

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

并且按钮在表单内:

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

对于我的其他观点,我有:

struct view5x: View {

    @Binding var goView1: Bool

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

我只是得到两个主体都声明不透明返回类型的错误。它不预览。

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    好的,您的观点中有类似的错误。要更好地了解它们,请查看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 }
    }
    

    所以,body 只是一个计算变量,它应该返回 some Viewview5x 中的错误是您将 2 个不同的视图放入其中,而不是 1。这里的解决方案是将它们嵌入到 VStack 例如:

    struct view5x: View{
    
        @Binding var goView1: Bool
    
        var body: some View{
    
            VStack {
                Text("TEST")
                Button(action: {
                    self.goView1.toggle()
                }) {
                    Text("Return")
                }
            }
    
        }
    }
    

    view4x 的问题是相似的——我认为,由于if...else 语句,不清楚什么视图返回正文。你可以用同样的方法修复它:

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

    另一种方法是说如果你将它们中的每一个包装到AnyView 中并在之前输入return,那么body 应该返回什么视图。在此示例中,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")
                })
            }
    
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 2020-03-13
      • 2010-11-06
      • 2021-03-06
      相关资源
      最近更新 更多