通过addSubview添加后调用父UIViewController中的方法

发布于 2024-11-07 21:56:27 字数 803 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拿命拼未来 2024-11-14 21:56:27

UIView 有一个 superview 属性,这似乎就是您正在寻找的。

此外,您可能不想像这样嵌套 UIViewController 的视图,除非您非常有意构建自定义包含视图控制器。请参阅http://blog.carbon Five.com/2011/03/09/abusing -uiviewcontrollers/

UIViews have a superview 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/

两仪 2024-11-14 21:56:27

您可能需要考虑是否可以通过使用 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.

失退 2024-11-14 21:56:27

当您将视图作为子视图添加到视图层次结构时,您将其放入响应程序链中。您可以沿着响应者链向上到达视图控制器,因为由 UIViewController 控制的 UIViewUIViewController 作为其 nextResponder< /代码>。

id object = theSubview;
do {
    object = [object nextResponder];
} while ( ![object isMemberOfClass:[YourViewController class]] );

// object has the view controller you need.

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 a UIViewController has the UIViewController as its nextResponder.

id object = theSubview;
do {
    object = [object nextResponder];
} while ( ![object isMemberOfClass:[YourViewController class]] );

// object has the view controller you need.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文