如何访问超级视图控制器? [英] How does one access a super's view controller?

查看:19
本文介绍了如何访问超级视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我会尽力解释这个问题:

I have a problem and I will try to explain the issue:

  1. 我有一个主要的 UIViewController(全屏)
  2. 我有一个辅助 UIViewController (setbounds)
  3. 我使用以下方法将辅助视图添加到了 mainView:

  1. I have one main UIViewController (Whole screen)
  2. I have one secondary UIViewController (setbounds)
  3. I added my secondary view to my mainView using this:

[mainController.view addSubview:secondaryController.view];   

  • 我创建了第三个控制器:modalController,我像这样将它添加到我的辅助控制器:

  • I created a third controller: modalController, I added it to my secondary controller like this:

    [secondaryController presentModalViewController:modalController animated:YES];
    

  • 我根据 modelController 中的一些事件进行微积分.

  • I make calculus based on some events inside of my modelController.

    我可以使用以下命令将消息从我的 modalController 发送到我的 secondaryController:

    I am able to send messages from my modalController to my secondaryController using:

    [[self parentViewController]  performSelector : @selector(myMethodInSecondaryController:) withObject : myObject afterDelay : .5];
    

    注意:self"对应于modalController

    我需要将myObject"传递给我的 mainController,但我无法从 secondaryController 引用我的 mainController.我试过这个:

    I need to pass "myObject" to my mainController, but i cant make reference to my mainController from the secondaryController. I tried this:

    [[self parentViewController] performSelector : @selector(myMethodInMainController:) withObject:myObject afterDelay : .5];
    

    注意:self"对应于secondaryController

    但它不起作用,我可以使用以下命令访问我的 mainController 视图:self.view.superview

    but it doesn't work, i have access to my mainController's view using : self.view.superview

    注意:self"是我的secondaryController

    但对它的控制器没有.

    but no to its controller.

    推荐答案

    在你的辅助控制器中,尝试

    In your secondary controller, try

    id mainViewController = [self.view.superview nextResponder];
    

    并检查这是否是您正在寻找的视图控制器.

    and check if this is the view controller you're looking for.

    Apple 的 -[UIResponder nextResponder] 文档:

    Apple's documentation of -[UIResponder nextResponder]:

    UIView 通过返回管理它的 UIViewController 对象(如果有)或它的超视图(如果没有)来实现这个方法

    UIView implements this method by returning the UIViewController object that manages it (if it has one) or its superview (if it doesn’t)

    这篇关于如何访问超级视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆