添加观察者(无参数):
[[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];
}
本文详细介绍了如何使用NSNotification在iOS应用中实现不同场景的通知机制。包括无参数及带参数的通知发送方式,以及如何添加观察者来响应这些通知。通过具体示例展示了NSNotification在实际项目中的运用。
1万+

被折叠的 条评论
为什么被折叠?



