NSNotification在UITextField的应用

本文详细介绍了UITextField在iOS开发中的用法,包括委托、通知机制及其在NSTextField中的实际应用,帮助开发者掌握如何利用这些特性进行高效编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 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                                                                                      }

                                                                                      }];









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值