#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,weak)id observer ;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/**
監(jiān)聽通知
ForName:通知名字
object:誰發(fā)出的通知
queue:決定block在哪個線程執(zhí)行,nil:在發(fā)布通知的線程中執(zhí)行 [NSOperationQueue mainQueue]:一般都是使用主隊(duì)列
usingBlock:block回調(diào)
*/
self.observer = [[NSNotificationCenter defaultCenter]addObserverForName:@"note" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"監(jiān)聽到通知%@---",[NSThread currentThread]);
}];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"note" object:nil];
});
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:_observer];
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者