如何限制TextField中字符输入的数目?

如何限制TextField中字符输入的数目,经过研究得到了2种方法。

1)通过监听键盘UITextFieldTextDidChangeNotification的消息,观察者在收到消息后执行相应的函数。
a)在viewDidLoad中,注册观察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changRegisterPhoneText) name:UITextFieldTextDidChangeNotification object:nil];

b)观察者在收到消息后执行相应的函数
- (void)changeRegisterPhoneText{
//执行相应控制字符数目的操作
}


2)通过TextField的代理事件
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//通过返回NO,禁止你输入字符,而通过返回YES,允许用户输入字符
}
该函数的相关解释如图片所示。 
图片:函数解释.png 
原文地址:https://www.cnblogs.com/iOS-kk/p/5207305.html