您的位置:首页 > 其它

【IPhone开发】NSNotificationCenter观察者模式给主线程发送通知

2012-09-03 11:28 489 查看
添加观察者(无参数):

[[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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: