通知中心

来源:互联网 发布:洲际酒店怎么样 知乎 编辑:程序博客网 时间:2024/06/10 02:48

    //通知中心没有依赖性,只要是两个页面存在

    //通知中心的使用

    //1. 获取通知中心,注册一个观察者和事件

 //这是一个单例类

    NSNotificationCenter *center = [NSNotificationCenterdefaultCenter];

    

    //再通知中心中,添加一个观察者和观察事件

    //参数1:负责相应事件的观察对象

    //参数2:一旦收到消息, 观察者要执行的方法

    //参数3:观察者要监听的事件

    //参数4:可以限定消息发出者

    [center addObserver:selfselector:@selector(receiveNotification:)name:@"上厕所"object:nil];

//收到通知中心的消息时,观察者(self)要调用的方法

- (void)receiveNotification:(NSNotification *)noti

{

    NSLog(@"%@", noti.object);

    self.view.backgroundColor = [UIColorblackColor];

}












- (void)buttonAction:(UIButton *)button

{

    //通知中心的使用

    //发送一个消息

    NSNotificationCenter *center = [NSNotificationCenterdefaultCenter];

    //参数1:发送消息的事件名

    //参数2:可以使用这个参数, 传递一个对象给观察者

    //参数3:一些消息的参数信息(系统用的比较多)

    [center postNotificationName:@"上厕所"object:@"香皂"userInfo:nil];

    

    [self.navigationControllerpopToRootViewControllerAnimated:YES];

    

}


0 0
原创粉丝点击