簡述:類似KVO,監(jiān)聽對象
系統(tǒng)的 Notification
- 例如:系統(tǒng)鍵盤的 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 通知回調:注冊時鏈接回調方法
// 此處 出現(xiàn)了 NSNotification 是系統(tǒng)創(chuàng)建了通知類。自定義也可以創(chuàng)建。
- (void)test:(NSNotification *)notification{
// ueerInfo 包含一些信息,用于后續(xù)處理
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 自定義通知 并發(fā)送通知
通知本身具有三個主要屬性: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
通知中心 添加監(jiān)聽 移除監(jiān)聽
/**************** 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