IOS UITextField 事件列表

//UITextFieldDelegate
import UIKit
class ViewController:UIViewController,UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view,typically from a nib.
let rect = CGRect(x:10, y:80, width:300, height:
)
let textField = UITextField(frame:rect)
textField.placeholder = “Bank card no”
textField.autocorrectionType =UITextAutocorrectionType.no
textField.returnKeyType = UIReturnKeyType.done
textField.clearButtonMode =UITextFieldViewMode.whileEditing
textField.keyboardType = UIKeyboardType.numberPad
textField.keyboardAppearance =
UIKeyboardAppearance.dark
textField.delegate = self
textField.borderStyle = UITextBorderStyle.line
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldBeginEditing(_ textField:UITextField) -> Bool
// return NO to disallow editing.
{
return true
}
func textFieldDidBeginEditing(_ textField:UITextField)
// became first responder
{
}
func textFieldShouldEndEditing(_ textField:UITextField) -> Bool
// return YES to allow editing to stopand to resign first responder status.NO to disallow the editing session to end
{
return true
}
func textFieldDidEndEditing(_ textField:UITextField)
// may be called if forced even if shouldEndEditing returnsNO (e.g.view removed from window) or endEditing:YES called
{
}
func textField(_ textField:UITextField,shouldChangeCharactersIn range:NSRange,replacementString string:String) -> Bool {
// return NO to not change text
return true
}
func textFieldShouldClear(_ textField:UITextField) -> Bool {
return true
}
func textFieldShouldReturn(_ textField:UITextField)-> Bool {
return true
}
}

//方法包括:
将要开始编辑状态时调用此协议方法。
编辑状态开始后调用此协议方法。
编辑状态将要结束后调用此协议方法。
编辑状态结束后调用此协议方法。
协议方法在文本将要输入的时候调用。
协议方法返回一个BOOL值,指明是否允许根据用户请求清除内容。
协议方法返回一个BOOL值,指明是否允许在按下Enter键时结束编辑。

猜你喜欢

转载自blog.csdn.net/weixin_34362875/article/details/90866444