公司測試說,用戶輸入了11位手機號碼后,在輸入12位時,提示他只能輸入11位,先不說這合不合產品需求,對于這個功能,怎么樣實現和滿足呢?
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
代理根本就沒有辦法滿足需求,所以應該怎么辦呢,所以出現了通知
UITextFieldNotification
UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;
添加監聽很簡單:
/**
* 監聽UITextField文字改變
*
* @param textFieldTextDidChange 文本改變時通知方法
*
* @param name UITextFieldTextDidChangeNotification(通知類型)
*
* @param object (可用于傳遞數據)
*/
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange) name:UITextFieldTextDidChangeNotification object:telephone];
監聽方法
- (void)textFieldTextDidChange
{
if(telephone.text.length > 11 ){
NSRange range = NSMakeRange(0, 11);
telephone.text = [telephone.text substringWithRange:range];
[self endEditing:YES];
[MBProgressUtil showToast:@"手機號碼最多為11位" inView:self];
}
}
到此,需求實現并滿足,以后想怎么監聽文本樞都不用擔心代理方法不夠用啦!
附:
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
- 如果有什么疑問,可以在評論區一起討論;
- 如果有什么不正確的地方,歡迎指導!
注:本文首發于 iHTCboy's blog,如若轉載,請注明來源。