0
- (void)viewDidLoad {
[super viewDidLoad];
//NSNotificationCenter第一坑
NSLog(@"current thread:%@",[NSThread currentThread]);
//在主线程中注册通知并转发消息
//add observer in main thread and deal in main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"current thread:%@",[NSThread currentThread]);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"notificaiton_1" object:nil];
});
//在子线程中发送通知
//post notification in another thread not in the same thread as main
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"notificaiton_1" object:nil];
});
// Do any additional setup after loading the view, typically from a nib.
}
//and this method never be called
- (void)handleNotification:(NSNotification *)notification{
//处理通知
//handle notification here
NSLog(@"current thread:%@",[NSThread currentThread]);
NSLog(@"handle notification");
}
Зачем вы хотите это сделать? Наиболее распространенная практика - опубликовать уведомление в главной теме ... – Subbu
да, напишите уведомление в главной теме, может быть, хорошая практика! но я хочу знать теорию внедрения NSNotification и почему нельзя вызывать метод уведомлений - (void) handleNotification: (NSNotification *). – lobster