關于監聽UITextField的問題

公司測試說,用戶輸入了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,如若轉載,請注明來源。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,556評論 25 708
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,255評論 4 61
  • 一日一景 深秋懷古情,深巷留遺存。 塔披青藤黃,廢墟寄遐想。
    吉光片羽_9bc2閱讀 155評論 1 3
  • 常常幻化人形,但變身蹩腳…
    小小吱吱叫閱讀 381評論 1 2