Accessing a Top Navigation Controller from a Subview Navigation Controller

前端 未结 3 1771
再見小時候
再見小時候 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: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.

提交回复
热议问题