我有以下情况:
SecondViewController
位于 FirstViewController
内。现在,我想向 FirstViewController
添加另一个子类,但是来自 secondViewController
类,如图所示:
我一直在寻找,但我不认为这是可能的。我已经尝试实例化 FirstViewController,访问“ View 到 subview ”并添加为 subview ,但这没有用:
FirstViewController *viewController = [[FirstViewController alloc] init];
[self.view addSubview:[viewController viewToAddAsSubView]];
任何提示/解决方案?
谢谢!
最佳答案
这实际上可以使用 NSNotificationCenter。示例如下:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(METHOD:) name:@"LISTEN_TO_VIEW_2" object:nil];
上面的代码应该放在 View 1 中以“监听” View 2 以向它发送一个通知,通知你应该执行 View 1 中的方法以在 View 1 中添加/编辑你想要的任何内容。
[[NSNotificationCenter defaultCenter] postNotificationName:@"LISTEN_TO_VIEW_2" object:nil];
上面的代码会将通知发送到 View 1。然后在 View 1 中,您将在某个地方使用这样的方法:
-(void)METHOD:(id)sender {
//do something here
}
关于objective-c - 隐藏/显示另一个类中的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11751667/