通过addSubview添加后调用父UIViewController中的方法
我有一个 UIViewController 正在创建另一个视图控制器,并将其视图添加为子视图:
在父 UIViewController 中:
SlateMoreView* subView = [[SlateMoreView alloc] initWithNibName:@"SlateMoreView" bundle:nil];
[self.view addSubview:subView.view];
然后我需要在父视图中从子视图调用方法。
当我使用 [self.navigationController PushViewController: subViewAnimated:YES] 添加子 UIViewController 时,我已经看到了如何做到这一点,因为我可以使用这种代码找到父级:
在子视图 UIViewController:
NSArray* viewControllerArray = [self.navigationController viewControllers]
int parentViewControllerIndex = [viewControllerArray count] - 2;
SlateView* slateView = [viewControllerArray objectAtIndex:parentViewControllerIndex];
...然后我可以给它发消息。但由于我使用 addSubView 手动添加了子视图,因此我无法执行此操作。
谁能想到我如何与我的父 UIViewController 对话?
谢谢!
I have a UIViewController that is creating another view controller, and adding its view as a subview:
In the parent UIViewController:
SlateMoreView* subView = [[SlateMoreView alloc] initWithNibName:@"SlateMoreView" bundle:nil];
[self.view addSubview:subView.view];
I then need to call a method from the subview, in the parent view.
I have seen how to do this when I am adding the sub UIViewController using [self.navigationController pushViewController: subView animated: YES], because I can find the parent using this kind of code:
In the sub view UIViewController:
NSArray* viewControllerArray = [self.navigationController viewControllers]
int parentViewControllerIndex = [viewControllerArray count] - 2;
SlateView* slateView = [viewControllerArray objectAtIndex:parentViewControllerIndex];
...and then I can send messages to it. But since I added the sub view manually by using addSubView, I can't do this.
Can anyone think of how I can talk to my parent UIViewController?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UIView
有一个superview
属性,这似乎就是您正在寻找的。此外,您可能不想像这样嵌套 UIViewController 的视图,除非您非常有意构建自定义包含视图控制器。请参阅http://blog.carbon Five.com/2011/03/09/abusing -uiviewcontrollers/
UIView
s have asuperview
property which seems to be what you are looking for.In addition you probably don't want to nest
UIViewController
's view like that unless you are very deliberately building a custom contain view controller. See http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/您可能需要考虑是否可以通过使用 NSNotifications 来解决您的问题。当感兴趣的侦听器(您的超级视图)需要了解的事件发生时,您可以从子视图发布通知。当超级视图收到通知时,它可以运行您想要的任何代码。子视图始终不需要知道父视图。
这是减少类之间依赖的一种方法。
您还可以使用委派作为另一种选择。
You might want to consider if your problem can be solved by using NSNotifications. You could post a notification from your subview when an event happens that interested listeners (your superview) need to know about . When the superview receives the notification, it can run whatever code you wish. All the while the subview never needs to know about the superview.
This is one way to make your classes less dependant on each other.
You could also use delegation as another option.
当您将视图作为子视图添加到视图层次结构时,您将其放入响应程序链中。您可以沿着响应者链向上到达视图控制器,因为由
UIViewController
控制的UIView
将UIViewController
作为其nextResponder< /代码>。
When you add your view as a subview to a view hierarchy, you put it in the responder chain. You can go up the responder chain to reach the view controller as a
UIView
controlled by aUIViewController
has theUIViewController
as itsnextResponder
.