两个不直接连接的 ViewController 之间的通信
Communication between two not directly connected ViewControllers
我想让ViewController F和ViewController C交流。
它们是这样排列的:
导航控制器 A 嵌入 ViewController B,它在容器视图中呈现 ViewController C。
从 ViewController B 到 NavigationController D 有一个 segue,它嵌入了 ViewController E 和 ViewController F(E 和 F 之间的 segue)。
我目前的工作解决方案如下:在必要的 ViewController 之间建立一个委托 "path":ViewController F 委托给 ViewController E,后者委托给ViewController B,最终将信息委托给ViewController C。
感觉必须有更简单的方法来做到这一点。
你能推荐一个吗?或许把"segue path"里面的ViewController C交给ViewController F,建立C和F之间的直接委托?
谢谢!
我会使用 NSNotification
在ViewController F:
- (void)sendData {
// Fire the notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedData"
object:nil];
}
在ViewController C:
- (void)viewDidLoad {
[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receivedDataNotification:)
name:@"ReceivedData"
object:nil];
}
- (void)receivedDataNotification:(id)object {
NSLog(@"Received Data!");
}
我想让ViewController F和ViewController C交流。 它们是这样排列的: 导航控制器 A 嵌入 ViewController B,它在容器视图中呈现 ViewController C。 从 ViewController B 到 NavigationController D 有一个 segue,它嵌入了 ViewController E 和 ViewController F(E 和 F 之间的 segue)。
我目前的工作解决方案如下:在必要的 ViewController 之间建立一个委托 "path":ViewController F 委托给 ViewController E,后者委托给ViewController B,最终将信息委托给ViewController C。
感觉必须有更简单的方法来做到这一点。
你能推荐一个吗?或许把"segue path"里面的ViewController C交给ViewController F,建立C和F之间的直接委托?
谢谢!
我会使用 NSNotification
在ViewController F:
- (void)sendData {
// Fire the notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedData"
object:nil];
}
在ViewController C:
- (void)viewDidLoad {
[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receivedDataNotification:)
name:@"ReceivedData"
object:nil];
}
- (void)receivedDataNotification:(id)object {
NSLog(@"Received Data!");
}