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

查看运行结果

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

上一篇:用SAS进行泊松,零膨胀泊松和有限混合Poisson模型分析


下一篇:基于Vue的Better-Scroll组件封装