访问模态视图控制器父级

时间:2011-07-18 15:16:02

标签: iphone ios

我正在以模态方式呈现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 ...

7 个答案:

答案 0 :(得分:89)

您可以通过以下方式访问父级:

self.presentingViewController

根据苹果文档:

  

提供此视图控制器的视图控制器(或其视图控制器)   最远的祖先。)

答案 1 :(得分:24)

我这样做的方法就是简单地创建一个委托。在AskPasswordViewController的标题中,输入

id delegate;

@property (nonatomic, assign) id delegate;

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

答案 2 :(得分:6)

你可以通过调用parentViewController来改进:

self.parentViewController.parentViewController ....等等,直到你找到合适的人。

答案 3 :(得分:2)

除了Vibhor Goyal - 有时,self.presentingViewController注册为NavigationController。因此,试试:

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

答案 4 :(得分:1)

在这里,我提出了从任何地方导航到根目录的通用方法。

  1. 您使用此类创建一个新的类文件,以便可以从项目的任何位置访问它:

    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)
                })
            }
        }
    }
    
  2. 从项目的任何位置使用:

    {
        ...
        SharedControllers.navigateToRoot(self)
        ...
    }
    

答案 5 :(得分: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];
               }
           }

答案 6 :(得分:0)

如果 class PropertiesJavaFormatConfig implements JavaFormatConfig { private final Properties properties; PropertiesJavaFormatConfig(Properties properties) { this.properties = properties; } @Override public IndentationStyle getIndentationStyle() { Object value = this.properties.get("indentation-style"); return (value != null) ? IndentationStyle.valueOf(value.toString().toUpperCase().trim()) : DEFAULT.getIndentationStyle(); } static JavaFormatConfig load(File file) throws IOException { try (InputStream inputStream = new FileInputStream(file)) { return load(inputStream); } } static JavaFormatConfig load(InputStream inputStream) throws IOException { Properties properties = new Properties(); properties.load(inputStream); return new PropertiesJavaFormatConfig(properties); } } 给了您意想不到的结果并且您不想摆弄它,请执行以下操作:

presentingViewController