2011-07-18 261 views
17

我以模态方式呈现ViewController。 如何访问父视图控制器?我的体系结构是TabBarController => VC1 => VC2 => VC3 => MVC1,我想从MVC1到VC3。访问模式视图控制器父

在VC3,我有这样的代码:

- (void) editAd{ 
    AskPasswordViewController *modalViewController = [[AskPasswordViewController alloc] initWithNibName:@"AskPasswordView" bundle:nil]; 

    NSLog(@"modalparent class=%@", [[modalViewController parentViewController] class]); 

    [self presentModalViewController:modalViewController animated:YES]; 
    [modalViewController release]; 
} 

我想这在MVC1:

- (void) sendRequest { 
    NSLog(@"classe : %@",[[self parentViewController] class]); 
} 

但它返回我的TabBarViewController ...

回答

21

我会这样做的方式是简单地创建一个委托。在AskPasswordViewController的头,把

id delegate; 

@property (nonatomic, assign) id delegate; 

合成它在实现文件。然后,在分配/初始化模态控制器之后,在出现之前,设置modalViewController.delegate = self;。然后在模态控制器中,您可以拨打self.delegate从显示它的视图控制器获取信息。我希望这有助于

+0

谢谢。这就是我最后所做的。但我想了解为什么父视图控制器是tabbar。这似乎很奇怪,我害怕在某个地方做错了事。 –

+1

我不完全确定为什么,但我的理论是,因为你正在呈现模态视图,所以它不一定是视图控制器的一个孩子,而是它的一部分。因此它的父母是视图的父母,因为它是一个扩展。它在这方面似乎有不同的行为,而不是通过导航控制器 – justin

+0

推送视图,它也适用于我。但是我必须让类“parentviewcontroller”的委托。我想访问“parentviewcontroller”的uitabbar。 – Chrizzz

5

您可以随时退回到更久远只需通过调用parentViewController像这样:

self.parentViewController.parentViewController ....等等等等你到达正确的一个。

+0

我有一个tabbar,它提供了navigationController,它推送了一些viewcontroller,它提供了恼人的viewController。 –

+0

以及你的问题给我们一个反馈,就像告诉我们你modalviewcontroller架构vc1 - > vc2 - > nvc - > tbvc然后告诉我们从哪里到哪里你想得到。 – Cyprian

+2

要打印acctual类名称使用:NSLog(@“clase:%@”,NSStringFromClass [[self parentViewController] class]));' – Cyprian

75

你可以通过调用访问父:

self.presentingViewController 

按照苹果的文档:(或其 最远的祖先)

所呈现这个视图控制器视图控制器

+4

虽然其他评论正确的时候,我认为这是故事板的方式(尤其是iOS 7以后) – Robin

+5

只是一个参考,这将是无效的viewDidLoad – user3344977

0
//You have to get the root, iterate through the root's ViewControllers and then ask for the ParentViewController. 
    UINavigationController ​*root = (UINavigationController*​)[[(AppDelegate*) [[UIApplication sharedApplication]delegate] window] rootViewController]; 
      for (UIViewController *VC in root.viewControllers) { 
       if([VC isKindOfClass:[YourParentViewController class]]){ 
        YourParentViewController* parent = (YourParentViewController*)VC; 
        [parent callMethod]; // your code here 
        [self dismissViewControllerAnimated:YES completion:nil]; 
       } 
      } 
1

在这里,我想出了通用l方法从任何地方导航到根。

  1. 你创建这个类一个新的类文件,以便它可以访问在你的项目中的任何地方:

    { 
        ... 
        SharedControllers.navigateToRoot(self) 
        ... 
    } 
    
:从您的项目中的任何地方

import UIKit 

class SharedControllers 
{ 
    static func navigateToRoot(viewController: UIViewController) 
    { 
     var nc = viewController.navigationController 

     // If this is a normal view with NavigationController, then we just pop to root. 
     if nc != nil 
     { 
      nc?.popToRootViewControllerAnimated(true) 
      return 
     } 

     // Most likely we are in Modal view, so we will need to search for a view with NavigationController. 
     let vc = viewController.presentingViewController 

     if nc == nil 
     { 
      nc = viewController.presentingViewController?.navigationController 
     } 

     if nc == nil 
     { 
      nc = viewController.parentViewController?.navigationController 
     } 

     if vc is UINavigationController && nc == nil 
     { 
      nc = vc as? UINavigationController 
     } 

     if nc != nil 
     { 
      viewController.dismissViewControllerAnimated(false, completion: 
       { 
        nc?.popToRootViewControllerAnimated(true) 
      }) 
     } 
    } 
} 
  • 用法

  • 1

    除了Vibhor Goyal - 有时,self.presentingViewController注册为NavigationControlle河因此,请尝试:

    if let vc = self.presentingViewController.childViewControllers.last as? YourViewController { 
    // do stuff here 
    }