【问题标题】:How to use form sections from separate file?如何使用来自单独文件的表单部分?
【发布时间】:2020-11-17 10:57:15
【问题描述】:

今年早些时候,我使用 SwiftUI 创建了一个应用程序。在 iOS 14 中运行时,此应用程序的一部分损坏了。我有一个创建和编辑表单,目标是重用它们共有的部分。以前它可以将这些部分放在 Group 中,因为 iOS 14 失败并将所有部分放在一个视图中。

目前的实现类似于这样:

struct CreateForm: View {
    var body: some View {
        Form {
            Section {
                Text("First section")
            }
            OtherSections()
        }
    }
}

struct OtherSections: View {
    var body: some View {
        Group { // <--- This is causing the issue
            Section {
                Text("First Detail Section")
            }

            // In real implementation there are some conditional sections
            if false {
                Section {
                    Text("Second Detail Section")
                }
            }
        }
    }
}

关于如何解决这个问题的任何建议?

【问题讨论】:

    标签: swiftui ios14


    【解决方案1】:

    尝试使用@ViewBuilder 而不是Group

    struct OtherSections: View {
        @ViewBuilder
        var body: some View {
            Section {
                Text("First Detail Section")
            }
    
            // In real implementation there are some conditional sections
            if false {
                Section {
                    Text("Second Detail Section")
                }
            }
        }
    }
    

    (还要确保OtherSections 符合View

    【讨论】:

    • 好吧,有点恼火我找不到那个。我有点期待类似的东西,但找不到。 OtherSections在实际实现中已经符合View
    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 2015-05-22
    • 1970-01-01
    • 2017-12-11
    • 2011-05-27
    • 2019-05-16
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多