自定义NSNotificationCenter的使用

1、在需要接收NSNotification的类里面的init方法中添加Observer,同时添加对应的处理方法,如:

在需要处理的类Class1的init方法中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationFunc:) name:NOTIFY_FUNC_DEMO object:nil];
这个用来注册一个接收NOTIFY_FUNC_DEMO消息的处理函数notificationFunc

同时Class内实现notificationFunc方法,该方法的参数里面,带的参数就是,notification过来的参数
- (void) notificationFunc:(NSNotification*)note {
    id notifyObject = [note object];
}

 

2、在需要进行消息传递的类的某个方法中进行消息通知

如在Class2中

[[NSNotificationCenter defaultCenter] postNotificationName: NOTIFY_FUNC_DEMO object:notifyObject];

notifyObject 中就是需要传递的消息或者值。

 

原文地址:https://www.cnblogs.com/easonoutlook/p/2642793.html