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

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

问题描述

我有一个问题,我会尝试解释这个问题:


  1. 我有一个主要的 UIViewController (整个屏幕)

  2. 我有一个次要 UIViewController (setbounds)

  3. 我使用此功能将我的次要资料检视加到我的 mainView

      [mainController.view addSubview:secondaryController.view]; 


  4. 我创建了第三个控制器: modalController ,我添加到我的第二个控制器像这样:

      [secondaryController presentModalViewController:modalController animated:YES]; 


  5. 我根据我的 modelController


  6. 我可以从 modalController code> secondaryController 使用:

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

    注意:self对应于 modalController


  7. 我需要将myObject传递给我的 mainController 但我不能参考从 secondaryController 中的 mainController 。我尝试这样:

      [[self parentViewController] performSelector:@selector(myMethodInMainController :) withObject:myObject afterDelay:。 

    注意:self对应于 secondaryController



    但它不工作,我有权访问我的mainController的视图使用: self.view.superview

    self是我的 secondaryController


,但不是其控制器。

解决方案

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

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

并检查这是否是您要查找的视图控制器。



Apple的文档 - [UIResponder nextResponder]


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



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

  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];   
    

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

    [secondaryController presentModalViewController:modalController animated:YES];
    

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

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

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

    NOTE: "self" corresponds to the modalController

  7. 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];
    

    NOTE: "self" corresponds to the secondaryController

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

    NOTE: "self" is my 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's documentation of -[UIResponder nextResponder]:

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

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

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