简述:类似KVO,监听对象
系统的 Notification
- 例如:系统键盘的 UIKeyboardDidChangeFrameNotification
- 1 注册通知:需要在通知中心 注册使用。
// 举例使用 输入框 弹出键盘。
UITextField *testField = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 100, 40)];
testField.backgroundColor = [UIColor yellowColor];
[self.view addSubview:testField];
// 通知中心
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
// 注册通知,以 SEL 方法回调
[center addObserver:self selector:@selector(test:) name:UIKeyboardDidChangeFrameNotification object:nil];
- 2 通知回调:注册时链接回调方法
// 此处 出现了 NSNotification 是系统创建了通知类。自定义也可以创建。
- (void)test:(NSNotification *)notification{
// ueerInfo 包含一些信息,用于后续处理
NSDictionary *userinfo = notification.userInfo;
NSLog(@"%@",userinfo);
- 3 移除通知
[center removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
自定义的通知
- 1 通知中心注册 自定义通知
// 使用 label 举例
self.testLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.testLabel.text = @"test";
[self.view addSubview:self.testLabel];
// 注册 自定义名称的通知
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(testWillChange:) name:@"testWillChangeNotification" object:self.testLabel];
[center addObserver:self selector:@selector(testDidChange:) name:@"testDidlChangeNotification" object:self.testLabel];
- 2 自定义通知 并发送通知
通知本身具有三个主要属性:name, object, userInfo
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
// 自定义通知: 名称, 对象, userInfo, 三个主要属性。这里做2 个示例:will 和 did
// willChange
NSNotification *testWillChangeNotification = [NSNotification notificationWithName:@"testWillChangeNotification" object:self.testLabel userInfo:@{@"key":self.testLabel.text}];
[center postNotification:testWillChangeNotification];
// 改变
self.testLabel.text = @"newTest";
// didChange
NSNotification *testDidlChangeNotification = [NSNotification notificationWithName:@"testDidlChangeNotification" object:self.testLabel userInfo:@{@"key":self.testLabel.text}];
[center postNotification:testDidlChangeNotification];
- 3 通知回调
- (void)testWillChange:(NSNotification *)notification{
NSDictionary *userinfo = notification.userInfo;
NSLog(@"%@",userinfo);
}
- (void)testDidChange:(NSNotification *)notification{
NSDictionary *userinfo = notification.userInfo;
NSLog(@"%@",userinfo);
}
- 4 移除通知
[center removeObserver:self name:@"testWillChangeNotification" object:self.testLabel];
[center removeObserver:self name:@"testDidlChangeNotification" object:self.testLabel];
其他
通知 本身
/**************** Notifications ****************/
@interface NSNotification : NSObject <NSCopying, NSCoding>
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@end
通知中心 添加监听 移除监听
/**************** Notification Center ****************/
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;
- (void)removeObserver:(id)observer;
- (id <NSObject>)addObserverForName:(nullable NSString *)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block NS_AVAILABLE(10_6, 4_0);
@end