其他分享
首页 > 其他分享> > SwiftUI系列教程第1章第8节:TextField

SwiftUI系列教程第1章第8节:TextField

作者:互联网

 

源码地址: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

查看运行结果

标签:username,教程,ContentView,SwiftUI,var,TextField,nickname,View
来源: https://blog.csdn.net/fzhlee/article/details/99700974