meaning-matters meaning-matters - 5 months ago 447
iOS Question

Check if view controller is presented modally, or pushed on a navigation stack

How can I, in my view controller code, differentiate between:


  • presented modally

  • pushed on navigation stack



Both
presentingViewController
and
isMovingToParentViewController
are
YES
in both cases, so are not very helpful.

What complicates things is that my parent view controller is sometimes modal, on which the to be checked view controller is pushed.

It turns out my issue is that I embed my
HtmlViewController
in a
UINavigationController
which is then presented. That's why my own attempts and the good answers below were not working.

HtmlViewController* termsViewController = [[HtmlViewController alloc] initWithDictionary:dictionary];
UINavigationController* modalViewController;

modalViewController = [[UINavigationController alloc] initWithRootViewController:termsViewController];
modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:modalViewController
animated:YES
completion:nil];


I guess I'd better tell my view controller when it's modal, instead of trying to determine.

Answer

Take with a grain of salt, didn't test.

- (BOOL)isModal {
     if([self presentingViewController])
         return YES;
     if([[self presentingViewController] presentedViewController] == self)
         return YES;
     if([[[self navigationController] presentingViewController] presentedViewController] == [self navigationController])
         return YES;
     if([[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]])
         return YES;

    return NO;
 }