How to get parent navigation controller in UIView

I created UITabBarControllerapplications in my account. where each element of the tab bar contains another UINavigationController, which loads the user UIViewControllerusing the NIB (using -pushViewController:). Inside one navigation controller, I load a custom class UIViewusing a custom NIB. This view is loaded several times inside UIViewController.

The user UIViewhas UIButtonthat if I touch it, I want to press the new one UIViewControlleron the stack. The problem is that I "lost" UINavigationControllerwhich contains UIViewController. I know that I should use delegates, but I did not understand who should, what class should be a delegate.

Thanks in advance!

+3
source share
2 answers

No .navigationController or .tabBarController will be available for the UIView or UIViewController that were created but not pushed onto the stack

Either create a property in the View (or ViewController) class, which is a UIViewController, which is optionally provided after initialization, or you can add a third argument to initWithNibName:bundle:

@interface CustomViewController : UIViewController
{ 
    UIViewController *owner;
}

@property (nonatomic, assign) UIViewController* owner;

@end

Then the owner of the ViewController:

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

Too bad .parentViewController is read-only, this would be a reasonable place for this.

+3
source

You can get this using a class UIApplication. Using this class, you can find where it viewControllerwill be placed first. Here is the link for your problem.

0
source

All Articles