UItextFieldのリスニングに関する質問

3675 ワード

会社のテストによると、ユーザーが11桁の携帯電話番号を入力した後、12桁を入力すると、11桁しか入力できないことを提示した.

UITextFieldDelegate

@protocol UITextFieldDelegate 

@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 
@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に先発して、もし転載するならば、出所を明記してください.