【IPhone开发】NSNotificationCenter观察者模式给主线程发送通报
【IPhone开发】NSNotificationCenter观察者模式给主线程发送通知
添加观察者(无参数):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"update_index_view" object:nil];
发送通知(无参数):
//根据网络状态,判断首页的显示内容 [[NSNotificationCenter defaultCenter] postNotificationName:@"update_index_view" object:nil userInfo:nil];
添加观察者(有参数):
/刷新首页UI [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mainThread_handleURLStartup:) name:@"startup_from_url" object:nil]; -(void) mainThread_handleURLStartup:(NSNotification *) note { NSDictionary *info = [note userInfo]; NSString *url_result = [[note userInfo] objectForKey:@"url_result"]; NSLog(@"url_result : %@",url_result); NSLog(@"start from url , information: %@", info); NSString *startupResult = [info objectForKey:@"url_result"]; NSLog(@"startup url result: %@", startupResult); }
发送通知(有参数):
NSString *appIndentier = @"kuainiao://"; NSRange range = NSMakeRange(0, [appIndentier length]); NSLog(@"_startupURL is %@", _startupURL); NSString *schema = [_startupURL substringWithRange:range]; if([schema isEqualToString:appIndentier]) { NSString *startupParam = [_startupURL substringFromIndex:[appIndentier length]]; NSLog(@"startupParam is %@",startupParam); NSDictionary *info = [NSDictionary dictionaryWithObject:startupParam forKey:@"url_result"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"startup_from_url" object:nil userInfo:info]; }