您的位置:首页 > 产品设计 > UI/UE

NSNotification在UITextField的应用

2016-05-02 12:00 393 查看
一 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中的使用

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


[[NSNotificationCenterdefaultCenter]
addObserverForName:UITextFieldTextDidBeginEditingNotification

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

                                                                                     if (Self == note.object)
{

                                                                                          //do something                                                                                      }

                                                                                  }];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: