基本使用
通知是有順序的屈留,先監(jiān)聽(tīng)再發(fā)送 才會(huì)收到信息
通知示例一:
// 接收通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(indexairportName) name:@"indexairportName" object:nil];
#pragma mark ===========接受通知
-(void)indexairportName{
/**處理通知代碼**/
}
//銷(xiāo)毀通知
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
//其他頁(yè)面-發(fā)出通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"indexairportName" object:nil];
通知示例二:
與示例一接受通知方法不同,銷(xiāo)毀方法也不同
@property (nonatomic,weak) id observe;
/**
* queue:block在哪個(gè)線程執(zhí)行测蘑,傳值 nil:在通知發(fā)布的線程中執(zhí)行
* block:接受通知后執(zhí)行操作
*/
_observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
/**處理通知代碼**/
}];
//銷(xiāo)毀通知
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:_observe];
}
//其他頁(yè)面-發(fā)出通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"indexairportName" object:nil];
多線程使用
示例一:多線程
- 接收通知處理代碼線程 由發(fā)出通知的線程決定
- 發(fā)通知(主線程)-監(jiān)聽(tīng)通知(子線程):接收通知代碼在主線程處理
- 發(fā)通知(子線程)-監(jiān)聽(tīng)通知(主線程):接收通知代碼在子線程處理
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationCode) name:@"noti" object:nil];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"noti" object:nil];
});
}
/** 監(jiān)聽(tīng)通知會(huì)調(diào)用方法
* 接收通知處理代碼線程 由發(fā)出通知的線程決定
* 發(fā)通知(主線程)-監(jiān)聽(tīng)通知(子線程):接收通知代碼在主線程處理
* 發(fā)通知(子線程)-監(jiān)聽(tīng)通知(主線程):接收通知代碼在子線程處理
*/
- (void)notificationCode{
dispatch_async(dispatch_get_main_queue(), ^{
//更新 UI 操作
});
NSLog(@"notificationCode:%@",[NSThread currentThread]);
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
示例二:多線程
- 接收通知處理代碼線程 由NSNotificationCenter的隊(duì)列決定
- 發(fā)通知(主線程)-監(jiān)聽(tīng)通知(并發(fā)隊(duì)列):接收通知代碼在子線程處理
- 發(fā)通知(子線程)-監(jiān)聽(tīng)通知(主隊(duì)列):接收通知代碼在主線程處理
@property (nonatomic,weak) id observe;
- (void)viewDidLoad {
[super viewDidLoad];
_observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"noti" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
/**處理通知代碼**/
NSLog(@"usingBlock:%@",[NSThread currentThread]);
}];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:_observe];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"noti" object:nil];
});
}
block 中回歸主線程方法:
1.多線程
dispatch_async(dispatch_get_main_queue(), ^{
//更新 UI 操作
});
2.設(shè)置隊(duì)列
_observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"noti" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
/**處理通知代碼**/
NSLog(@"usingBlock:%@",[NSThread currentThread]);
}];