6

In my app I can call a UIViewControle in both mode: Push and ModalDialog.

How can I determine, once the UIViewController is active, if has been called as Push or Modal Dialog ?

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
Fulkron
  • 1,013
  • 2
  • 14
  • 22
  • possible duplicate of [Is it possible to determine whether ViewController is presented as Modal?](http://stackoverflow.com/questions/2798653/iphone-is-is-possible-to-determine-wheteher-viewcontroller-is-presented-as-moda) – Bo Persson Jul 22 '12 at 19:43
  • I used the solution here, which handles cases where the view controller is presented inside a navigation/tab controller http://stackoverflow.com/a/6349300 – tristanl Jul 22 '12 at 19:18

4 Answers4

6

You can check modalViewController property of parent view controller like this:

if ([self.parentViewController.modalViewController isEqual:self]) {
    NSLog(@"Modal");
} else {
    NSLog(@"Push");
}

Just remember to check it after the view has been pushed/presented.

suda
  • 2,604
  • 1
  • 27
  • 38
  • 7
    This doesn't work from IOS 5+ because UIViewController does not answer for parentViewController anymore, but to presentingViewController instead. See http://stackoverflow.com/questions/2798653/iphone-is-is-possible-to-determine-wheteher-viewcontroller-is-presented-as-moda – Tieme Apr 10 '12 at 09:58
5

This works for me:

   if(self.presentingViewController){
        //modal view controller 

    }else{


    }
sash
  • 8,423
  • 5
  • 63
  • 74
2

In case you haven't figured this out yet, I'll share my situation and how I detected whether I'm in a modal view controller.

I have a segue which presents a view controller modally. This view controller is embedded in a navigationController so that I inherit all the good UIBarButtonItem capabilities.

if ([self.parentViewController.presentingViewController.modalViewController isEqual:self.parentViewController]) {
   NSLog(@"I'm in a modal view controller!");
}

Hope this helps

Caborca87
  • 187
  • 1
  • 16
0

The thing is the viewController may not be presented itself, but the collection view controller that contains it is. Maybe more general case will be useful for somebody:

- (BOOL)isModal {

    return self.presentingViewController.presentedViewController == self
    || (self.navigationController != nil && self.navigationController.presentingViewController.presentedViewController == self.navigationController)
    || [self.tabBarController.presentingViewController isKindOfClass:[UITabBarController class]]; 
}
Miroslav
  • 546
  • 2
  • 9
  • 22