How to get parent navigation controller in UIView

ぃ、小莉子 提交于 2019-12-23 15:36:15

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5935186/how-to-get-parent-navigation-controller-in-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!