iPhone - 访问父视图控制器?
我有2个视图控制器A,B
- 视图控制器A正在显示 屏幕上
- ViewController B的视图是 添加到ViewController的视图中 A.
如何在不传递指针的情况下从 ViewController B 访问 ViewController A?
self.parentViewController 不起作用它返回 null
I have 2 viewcontrollers A, B
- ViewController A is being displayed
on the screen - The view of ViewController B is
added to the view of ViewController
A.
How can i access ViewController A from ViewController B without passing a pointer?
self.parentViewController does not work it returns nuil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么viewController B的视图会被添加到viewController A的视图中呢?如果您打算向 viewController A 添加一个视图,您可以通过分配一个新的 UIView 并将其显示在当前视图的顶部来实现。
另一方面,如果您尝试从一个视图控制器转换到另一个视图控制器,即从 A 到 B,然后再返回到 A,则应该使用 Apple 文档中的视图控制器编程指南中指定的 UINavigationController。
来源: 视图控制器编程指南
编辑:如果你想使用
[[selfparentViewController]yourMethodHere];
你将需要一个视图层次结构,为此你可以使用UINavigationController。在您当前的方案中,没有构建层次结构,因此没有父视图控制器。Why is the view of viewController B added to the view of viewController A? If you are meaning to add a view to viewController A, you can do that by allocating a new UIView and displaying that on top of the current view.
On the other hand if you are trying to transition from one view controller to another, i.e going from A to B and then back to A, you should use a UINavigationController as specified in the View Controller Programming Guide in the Apple Documentation.
Source: View Controller Programming Guide
EDIT: If you want to use
[[self parentViewController] yourMethodHere];
you will need a view hierarchy, for which you can use the UINavigationController. In your current scheme there is no hierarchy built so there is no parent view controller.这应该可以工作,尽管它不是很优雅:
This should work though its not very elegant: