简体   繁体   中英

How to get parent navigation controller in UIView

I have created a UITabBarController in my app delegate. where each tab bar item holds a different UINavigationController that loads a custom UIViewController with a NIB (using -pushViewController: ). Inside one of the navigation controller, I load a custom UIView class with a custom NIB also. This view is loaded multiple times inside the UIViewController .

The custom UIView has a UIButton , that on the event of touching it, I want to push a new UIViewController on the stack. Problem is that I 'lost' the UINavigationController that holds the UIViewController . I know I should use delegates, but haven't figured out who should which class should be the delegate.

Thanks in advance!

Neither .navigationController or .tabBarController will be available for a UIView or UIViewController that's been created but not pushed onto a stack

Either create a property on your View (or ViewController) class that is a UIViewController that is provided optionally after initialization or you could add a third argument to initWithNibName:bundle:

@interface CustomViewController : UIViewController
{ 
    UIViewController *owner;
}

@property (nonatomic, assign) UIViewController* owner;

@end

Then in the owner ViewController:

CustomViewController *cvc = [[CustomViewController alloc] initWithNibNamed:nil bundle:nil];
cvc.owner = self;

It's too bad .parentViewController is read-only, this would be the sensible place for this.

You can get this using UIApplication class. Using this class you can find which viewController is placed at first. Here is the solution link for your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM