美文网首页
SwiftUI—制作一份漂亮而强大的表单

SwiftUI—制作一份漂亮而强大的表单

作者: anny_4243 | 来源:发表于2020-07-16 11:50 被阅读0次

    原文链接:https://github.com/fzhlee/SwiftUI-Guide#-%E7%AC%AC13%E8%8A%82form-basic-

    SwiftUI提供了强大而快速的表单制作功能。

    示例代码:

    struct ContentView : View {
    
        private var languages = ["Swift", "Objective-C"] //拾取器的数据源
        @State private var selectedLanguage = 0 //作为拾取器里处于选择状态的选项的索引值,和界面中的Picker进行数据绑定
        @State var workingYear: Double = 2 //滑杆视图的数值,和界面中的滑杆视图进行数据绑定 
        @State var enableNotification = false //形状视图的数值,和界面中的开关视图进行数据绑定
    
        var body: some View {
            NavigationView {
                Form { //添加一个表单
                    Picker(selection: $selectedLanguage, label: Text("Languages")) {
                       ForEach(0 ..< languages.count) {
                        Text(self.languages[$0]).tag($0)
                       }
                    }.pickerStyle(SegmentedPickerStyle())
                    HStack{
                        Text("Working years")
                        Slider(value: $workingYear, in: 1...10, step: 1)
                    }
                    
                    Toggle(isOn: $enableNotification) {
                        Text("Enable Notification")
                    }
    
                    Button(action: {
                        print("Your programming language: \(self.languages[self.selectedLanguage])")
                        print("Your working years: \(Int(self.workingYear))")
                        print("Enable notification: \(self.enableNotification)")
                    }) {
                        Text("Submit")
                    }
                }.navigationBarTitle(Text("Profiles"))
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:SwiftUI—制作一份漂亮而强大的表单

          本文链接:https://www.haomeiwen.com/subject/nhhphktx.html