SwiftUI—制作一份漂亮而强大的表单
2020-07-16 本文已影响0人
anny_4243
原文链接: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"))
}
}
}