`
mmdev
  • 浏览: 12919489 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

【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]; 
            }




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics