开发者

Accessing a Top Navigation Controller from a Subview Navigation Controller

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开发者_JS百科 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];
}


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.


You can use the follow instruccion:

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

It works for me :D


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 ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜