源码地址:https://github.com/fzhlee/SwiftUI-Guide
TextField类似于UIKit中的UITextField,用于实现用户的文字内容的输入。
示例代码:
struct ContentView : View {
@State var username : String
@State var nickname : String
var body: some View {
VStack{
Text("Your username is \(username)!")
Text("Your nickname is \(nickname)!")
TextField("User Name", text: $username, onEditingChanged: { (value) in
print("onEditingChanged:\(self.username)")
}) {
print("onCommit:\(self.username)")
}.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("Nick Name", text: $nickname)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
.padding()
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView(username: "", nickname: "")
}
}
#endif
查看运行结果