从子视图导航控制器访问顶部导航控制器 [英] Accessing a Top Navigation Controller from a Subview Navigation Controller

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

问题描述

我的观点和控制器设置如此。

I have a my views and controllers set up like so.


  1. 标签/栏控制器

  2. 1.是根视图控制器

  3. 2.是一个以编程方式创建的导航控制器,在根视图控制器中显示为子视图。

我要做的是访问顶部标签栏/导航控制器,以便我可以将视图推到它上面。

What I am trying to do is access the top tab bar/navigation controller so that i can push a view onto it.

我尝试过parentViewController,但它只是把视图推到了编程的导航控制器上。

I tried parentViewController but all it did was push the view onto the programmed nav controller.

有什么建议吗?

这就是我设置我的根视图控制器的方法:

This is how i set up my root view controller:

  -(void)viewDidAppear:(BOOL)animated{
    NSLog(@"ROOT APPEARED");
    [super viewDidAppear:animated];

    WorklistViewController *worklistController = [[WorklistViewController alloc]initWithNibName:@"WorklistView" bundle:[NSBundle mainBundle]];
    UINavigationController *worklistNavController = [[UINavigationController alloc] initWithRootViewController:worklistController];
    worklistNavController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    worklistNavController.view.frame = watchlistView.frame;
    [worklistNavController.topViewController  viewDidLoad];
    [worklistNavController.topViewController  viewWillAppear:YES];
    [self.view addSubview:worklistNavController.view];

    GetAlertRequestViewController *alertsController = [[GetAlertRequestViewController alloc]initWithNibName:@"AlertsView" bundle:[NSBundle mainBundle]];
    UINavigationController *alertsNavController = [[UINavigationController alloc] initWithRootViewController:alertsController];
    alertsNavController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    alertsNavController.view.frame = alertsView.frame;
    [alertsNavController.topViewController  viewDidLoad];
    [alertsNavController.topViewController  viewWillAppear:YES];
    [self.view addSubview:alertsNavController.view];
}


推荐答案

嵌套的ViewController(即,在由实际在NavController堆栈上的ViewController控制的视图内部,不能直接访问其父视图的控制器是其堆栈成员的UINavigationController。这是句子中的一个,但它的意义是:你不能从这里到达那里。

A nested ViewController (ie, inside a view controlled by a ViewController that's actually on the NavController stack) doesn't have direct access to the UINavigationController that its parent's view's controller is a stack member of. That's one MOUTHFUL of a sentence, but the sense of it is: you can't get there from here.

相反,你必须通过应用程序的NavController来获取App委托。

Instead you've got to get at the app's NavController via the App delegate.

YourAppDelegate *del = (YourAppDelegate *)[UIApplication sharedApplication].delegate;
[del.navigationController pushViewController:nextViewController animated:YES];

您正在使用您的UIApplication的单身人士(包含有关您的应用程序的各种有用信息),一个指向AppDelegate的.delegate属性,它包含对NavigationController的引用。

You're using your UIApplication's singleton (contains all sorts of good info about your app), which has a .delegate property pointing to the AppDelegate, and that contains a reference to the NavigationController.

这就是基于导航的应用程序Xcode模板设置NavController所有权的方式,无论如何。 YMMV如果你自己推出 - 虽然如果你这样做,你可能不需要问这个问题。

This is how the "Navigation-based Application" Xcode template sets up NavController ownership, anyway. YMMV if you rolled your own--though if you did, you probably wouldn't need to ask this question.

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

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