公司測(cè)試說居夹,用戶輸入了11位手機(jī)號(hào)碼后,在輸入12位時(shí)本冲,提示他只能輸入11位准脂,先不說這合不合產(chǎn)品需求,對(duì)于這個(gè)功能檬洞,怎么樣實(shí)現(xiàn)和滿足呢狸膏?
UITextFieldDelegate
@protocol UITextFieldDelegate <NSObject>
@optional
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
@end
代理根本就沒有辦法滿足需求,所以應(yīng)該怎么辦呢添怔,所以出現(xiàn)了通知
UITextFieldNotification
UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;
添加監(jiān)聽很簡(jiǎn)單:
/**
* 監(jiān)聽UITextField文字改變
*
* @param textFieldTextDidChange 文本改變時(shí)通知方法
*
* @param name UITextFieldTextDidChangeNotification(通知類型)
*
* @param object (可用于傳遞數(shù)據(jù))
*/
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange) name:UITextFieldTextDidChangeNotification object:telephone];
監(jiān)聽方法
- (void)textFieldTextDidChange
{
if(telephone.text.length > 11 ){
NSRange range = NSMakeRange(0, 11);
telephone.text = [telephone.text substringWithRange:range];
[self endEditing:YES];
[MBProgressUtil showToast:@"手機(jī)號(hào)碼最多為11位" inView:self];
}
}
到此湾戳,需求實(shí)現(xiàn)并滿足闷板,以后想怎么監(jiān)聽文本樞都不用擔(dān)心代理方法不夠用啦!
附:
UITextViewDelegate
@protocol UITextViewDelegate <NSObject, UIScrollViewDelegate>
@optional
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
- (void)textViewDidBeginEditing:(UITextView *)textView;
- (void)textViewDidEndEditing:(UITextView *)textView;
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
- (void)textViewDidChange:(UITextView *)textView;
- (void)textViewDidChangeSelection:(UITextView *)textView;
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);
@end
#UITextViewNotification通知
UIKIT_EXTERN NSString * const UITextViewTextDidBeginEditingNotification;
UIKIT_EXTERN NSString * const UITextViewTextDidChangeNotification;
UIKIT_EXTERN NSString * const UITextViewTextDidEndEditingNotification;
~nice
- 如果有什么疑問院塞,可以在評(píng)論區(qū)一起討論遮晚;
- 如果有什么不正確的地方,歡迎指導(dǎo)拦止!
注:本文首發(fā)于 iHTCboy's blog县遣,如若轉(zhuǎn)載,請(qǐng)注明來源汹族。