简体   繁体   English

从表单 (SwiftUI) 中的选择器上选择时扩展“其他”类别的应用程序

[英]App to expand 'Other' category when selected on picker from a form (SwiftUI))

I want to provide the user with a list of categories (via picker included in a form) and the last category listed is 'Other'.我想为用户提供一个类别列表(通过表单中包含的选择器),最后列出的类别是“其他”。 If the user selects 'Other', the app is to add a field to the form for the user to enter in the new category name (which would be saved for future references).如果用户选择“其他”,应用程序将在表单中添加一个字段,供用户输入新的类别名称(将保存以供将来参考)。

I have been able to have the picker list presented:我已经能够显示选择器列表:

Section {
    Picker(selection: $courseIndex, label: Text("Course")) {
        ForEach(0 ..< courses.count) {
            Text(self.courses[$0]).tag($0)
        } //ForEach
    } // Picker                    
} // Section - Course

But I am unable to determine how to have the new field presented for the user to enter in the new category.但我无法确定如何为用户提供新字段以输入新类别。

Thanks.谢谢。

You can add use if in the BodyBuilder, example:您可以在 BodyBuilder 中添加 use if ,例如:

struct ContentView: View {
    @State var courseIndex: Int = 2
    
    let courses = ["Course 1", "Course 2", "Other"]
    
    var body: some View {
        VStack {
            Section {
                Picker(selection: $courseIndex, label: Text("Course")) {
                    ForEach(0 ..< courses.count) {
                        Text(self.courses[$0]).tag($0)
                    }
                }
            }
        
            if courseIndex == 2 {
                Text("Show Extra")
            }
        }
    }
}

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

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