This question already has answers here:
Send and receive messages through NSNotificationCenter in Objective-C?
(6个答案)
7年前关闭。
我看到了几个在同一类中添加观察者和句柄的示例,但是我想知道的是是否有可能在第一个 View Controller 中添加观察者并在第二个 View Controller 中进行处理?
我想不断地从第一个 View Controller 发送距离并在第二个 View Controller 中处理它。
将第二个 View Controller 添加为 subview :
就像在android中播放一样。
其次,如下所示从第二个 View Controller 发布通知。
系统将通知广播给所有听众。
(6个答案)
7年前关闭。
我看到了几个在同一类中添加观察者和句柄的示例,但是我想知道的是是否有可能在第一个 View Controller 中添加观察者并在第二个 View Controller 中进行处理?
我想不断地从第一个 View Controller 发送距离并在第二个 View Controller 中处理它。
将第二个 View Controller 添加为 subview :
addSubview
,addChildViewController
。就像在android中播放一样。
最佳答案
是的,有可能。 NSNotificationCenter
完全以这种方式工作。
首先,您将必须在第一个 View Controller 中注册监听器,如下所示。
-(void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(somethingHappens:) name:@"notificationName" object:nil];
}
-(void)somethingHappens:(NSNotification*)notification
{
}
其次,如下所示从第二个 View Controller 发布通知。
[[NSNotificationCenter defaultCenter] postNotificationName:@"notificationName" object:obj];
系统将通知广播给所有听众。
10-08 03:17