views:

221

answers:

3

Is it possible to check inside ViewController class that it is presented as modal view controller?

+2  A: 

If there isn't, you can define a property for this (presentedAsModal) in your UIViewController subclass and set it to YES before presenting the ViewController as a modal view.

childVC.presentedAsModal = YES;
[parentVC presentModalViewController:childVC animated:YES];

You can check this value in your viewWillAppear override.

I believe there isn't an official property that states how the view is presented, but nothing prevents you from creating your own.

hgpc
RIght and this is what i have did but I was looking for some other neat solution. Thanks.
lukewar
+3  A: 

This should work.

if(self.parentViewController.modalViewController == self)…
kubi
Unfortunately this does not work. It was my first try. But returned modalViewController ins nil :(.
lukewar
If you just get 'self.parentViewController' does it return the correct parent object?
kubi
The problem might be that your UIViewController subclass is inside a UINavigationController or a UITabBarController (or both), in which case you might need to dig a bit more in the view hierarchy to find out the parent that was presented as a modal view controller.
hgpc
A: 

A hack like this might work.

UIViewController* child = self;
UIViewController* parent = child.parentViewController;
while (parent && parent.modalViewController != child) {
    child = parent;
    parent = child.parentViewController;
}
if (parent) {
    // A view controller in the hierarchy was presented as a modal view controller
}

However, I think my previous answer is a cleaner solution.

hgpc