通知 - NSNotification

簡述:類似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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容