从子视图导航控制器访问顶部导航控制器

发布于 2024-09-07 16:20:31 字数 1576 浏览 4 评论 0原文

我有一个像这样设置的视图和控制器。

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

我想做的是访问顶部选项卡栏/导航控制器,以便我可以将视图推送到上面。

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

有什么建议吗?

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

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

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

  1. A Tab/Bar controller
  2. Within 1. is a root view controller
  3. within 2. is a programmatically created navigation controller, that is displayed as a subview in the root view controller.

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

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

any suggestions?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一百个冬季 2024-09-14 16:20:31

嵌套的 ViewController(即,在由实际上位于 NavController 堆栈上的 ViewController 控制的视图内)无法直接访问其父视图的控制器是其堆栈成员的 UINavigationController。这句话虽然拗口,但其含义是:你无法从这里到达那里。

相反,您必须通过应用程序委托访问应用程序的 NavController。

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

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

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

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.

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

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.

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.

旧话新听 2024-09-14 16:20:31

您可以使用以下说明:

[(UINavigationController *)self.view.window.rootViewController pushViewController:vc animated:YES];

它对我有用:D

You can use the follow instruccion:

[(UINavigationController *)self.view.window.rootViewController pushViewController:vc animated:YES];

It works for me :D

忆依然 2024-09-14 16:20:31

查看 UIViewController 的 navigationController 和 tabBarController 属性。这些将返回给定 UIViewController“所属”的相应 navigationController 或 tabBarController。

所以你可以这样做:

[customController.navigationController pushViewController:newController animated:YES];
// Similarly for tabBarController ...

Have a look at UIViewController's navigationController and tabBarController properties. These will return the corresponding navigationController or tabBarController that the given UIViewController 'belongs' to.

So you can do something like:

[customController.navigationController pushViewController:newController animated:YES];
// Similarly for tabBarController ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文