通知的用法

来源:互联网 发布:古墓丽影崛起帧数优化 编辑:程序博客网 时间:2024/05/21 07:47

NSNotificationCenter是专门供程序中不同类间的消息通信而设置的。

1.注册通知
NSNotificationCenter* notification = [NSNotificationCenter defaultCenter];
    [notification addObserver:self selector:@selector(change:) name:@"changeImage" object:nil];

   

//addObserver 这个是观察者,就是说在什么地方接收通知

//selector这个是收到通知后,调用何种方法;

//name:这个是通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。


2.处理消息
- (void) change:(NSNotification*)notification
{
    id  obj = [notification object];//获取到传递的对象
}
3.dealloc里注销通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
4发送通知(在需要发送通知的类里)
[[NSNotificationCenter defaultCenter] postNotificationName:@"changeImage" object:self];

//object:这个对象是自身,你要传谁的值


原创粉丝点击