Skip to content

Instantly share code, notes, and snippets.

@alexito4
Created October 11, 2012 21:04
Show Gist options
  • Save alexito4/3875476 to your computer and use it in GitHub Desktop.
Save alexito4/3875476 to your computer and use it in GitHub Desktop.
Checking whether ViewController is presented as Modal
// UIViewController+ModalCheck.h
//
// Created by Felipe Sabino on 06/14/11
#import <UIKit/UIKit.h>
@interface UIViewController (ModalCheck)
-(BOOL)isPresentedAsModal;
@end
// UIViewController+ModalCheck.m
//
// Created by Felipe Sabino on 06/14/11
#import "UIViewController+ModalCheck.h"
@implementation UIViewController (ModalCheck)
-(BOOL)isPresentedAsModal {
BOOL isModal = ((self.parentViewController && self.parentViewController.modalViewController == self) ||
//or if I have a navigation controller, check if its parent modal view controller is self navigation controller
( self.navigationController && self.navigationController.parentViewController && self.navigationController.parentViewController.modalViewController == self.navigationController) ||
//or if the parent of my UITabBarController is also a UITabBarController class, then there is no way to do that, except by using a modal presentation
[[[self tabBarController] parentViewController] isKindOfClass:[UITabBarController class]]);
//iOS 5+
if (!isModal && [self respondsToSelector:@selector(presentingViewController)]) {
isModal = ((self.presentingViewController && self.presentingViewController.modalViewController == self) ||
//or if I have a navigation controller, check if its parent modal view controller is self navigation controller
(self.navigationController && self.navigationController.presentingViewController && self.navigationController.presentingViewController.modalViewController == self.navigationController) ||
//or if the parent of my UITabBarController is also a UITabBarController class, then there is no way to do that, except by using a modal presentation
[[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]]);
}
return isModal;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment