How a view controller can "embed" itself in a UINavigationController - especially if it is contained in a container view controller

Sorry for the long story -

I have some questions about the proper use of the navigation controller hierarchy.

Everyone UIViewControllerhas the propertyself.navigationController

But not every view controller is represented with a navigation controller, so this property may be null.

Moreover -

Some view controllers expect to be able to:

[self.navigationController presentViewController:nextViewController];

but if it itself is not contained in the hierarchy of the navigation controller, this expression will do nothing.

So, the trainee, the view controller, can check whether it was built into the navigation controller:

if (self.navigationController == nil)
{
  // create a UINavigationController with a root view controller - the next view controller
  UINavigationController *nav = [UINavigationController alloc] initWithRootViewController:nextViewController];
  [self presentViewController:nav];
}

- , , . :

[self presentViewController:nextViewController];

?

- , -

, UINavigationController -

, , .. .

- self.navigationController == nil - "" , ?

:

iOS , , UITabBarController, UIViewNavigationController - - "" "" .

( ), UITabBar .

container

(, " " ) - , .

- .

- , UINavigationController, ​​(-nil), - ..

"" ( ) -

navigationController ?

+4
1

, place-holder (self.mainView) viewControllers, . NavigationController, . , self.navigationController == nil .

, , viewController , .

if (self.currentController) {
    [self.currentController willMoveToParentViewController:nil];
    [[self.currentController view] removeFromSuperview];
    [self.currentController removeFromParentViewController];
    self.currentController = nil;
}


switch (tag) {
    case 0:
        self.currentController = [self.storyboard instantiateViewControllerWithIdentifier:@"AboutViewController"];
        break;

    case 1: {
        UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"EventsViewController"]];
        self.currentController = controller;
    }
        break;
}


if (self.currentController) {
    [self addChildViewController:self.currentController];
    [self.mainView addSubview:self.currentController.view];
    [self.currentController didMoveToParentViewController:self];
    [self adjustMainView];
    [self.view layoutIfNeeded];
}
0

Source: https://habr.com/ru/post/1533687/


All Articles