NSNotification在UITextField的应用

来源:互联网 发布:梦洁床垫怎么样知乎 编辑:程序博客网 时间:2024/05/19 13:45
一 UITextField

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextInput,NSCoding

@end

//UITextField的委托

@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

//UITextField通知

UIKIT_EXTERNNSString *const UITextFieldTextDidBeginEditingNotification;

UIKIT_EXTERNNSString *const UITextFieldTextDidEndEditingNotification;

UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;


二NSNotification


@interface NSNotification :NSObject <NSCopying,NSCoding>

@end


/**************** Notification Center****************/


@interface NSNotificationCenter :NSObject {

    @package

    void *__strong _impl;

    void *__strong _callback;

    void *_pad[11];

}


+ (NSNotificationCenter *)defaultCenter;


- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullableNSString *)aName object:(nullableid)anObject;


- (void)postNotification:(NSNotification *)notification;

- (void)postNotificationName:(NSString *)aName object:(nullableid)anObject;

- (void)postNotificationName:(NSString *)aName object:(nullableid)anObject userInfo:(nullableNSDictionary *)aUserInfo;


- (void)removeObserver:(id)observer;

- (void)removeObserver:(id)observer name:(nullableNSString *)aName object:(nullableid)anObject;


- (id <NSObject>)addObserverForName:(nullableNSString *)name object:(nullableid)obj queue:(nullableNSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))blockNS_AVAILABLE(10_6, 4_0);

    // The return value is retained by the system, and should be held onto by the caller in

    // order to remove the observer with removeObserver: later, to stop observation.


@end


三NSNotification在NSTextField中的使用

  1. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldTextDidChange) name:UITextFieldTextDidChangeNotification object:password];


  2. [[NSNotificationCenterdefaultCenter]addObserverForName:UITextFieldTextDidBeginEditingNotification

                                                                                     object:nilqueue:nilusingBlock:^(NSNotification *note) {

                                                                                         if (Self == note.object) {

                                                                                              //do something                                                                                      }

                                                                                      }];









0 0
原创粉丝点击