Accessing a Top Navigation Controller from a Subview Navigation Controller

前端 未结 3 1766
再見小時候
再見小時候 2021-02-04 07:26

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 create
相关标签:
3条回答
  • 2021-02-04 07:28

    You can use the follow instruccion:

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

    It works for me :D

    0 讨论(0)
  • 2021-02-04 07:37

    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.

    0 讨论(0)
  • 2021-02-04 07:53

    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 讨论(0)
提交回复
热议问题