Reputation: 18247
Here is what I'm trying to do. I'm creating a routing app that should handle request from the Apple map app. The map app does so by sending the following message to the app delegate
- (BOOL) application: (UIApplication *) application
openURL:(NSURL *) url
sourcApplication:(NSString *)sourceApplication
annotation:(id)annotation
The thing is, my app could already be executing and be any state at this point. It could be several layers deep in the navigation stack, it could also be that there is just one root controller in the navigation stack, but a modal view is currently covering over it.
Where ever it is, I want to pop all view controllers to keep just one top-most view controller, and this I know how to do so. But I also want to dismiss any modal view if it is there, and I don't know how to do it.
How would I know if a modal view (or if there is more than one possible model views, which isn't the case, but I'm asking it for the sake of the question.) is covering it? And I want to dismiss it? Is it accessible from the app delegate directly? Right now I'm just keeping a pointer reference of the modal view controller in that top-most view controller of the navigation stack, and dismiss the view controller if the reference is not nil. The app delegate simply asks the top-most view controller to do this job.
Is this proper?
Upvotes: 1
Views: 1312
Reputation: 8147
If you're using a navigation controller, you can easily access its topViewcontroller
and check if it has presented another view controller from your application delegate class. Up until iOS 6.0, the accessor you'd like to use for that would be 'modalViewController', after that it's marked as deprecated, so you should use presentedViewController
.
An example of such a check would be something like this:
// after rearranging view controller hierarchy, check for modal view controllers
UIViewController *topVc = [navigationController topViewController];
if ([vc presentedViewController] != nil) {
[vc performSelector:@selector(dismissModalViewControllerAnimated:)
withObject:[NSNumber numberWithBool:YES]
afterDelay:0.5];
}
Invoking the dismissModalViewControllerAnimated:
with a bit of delay (you might want to tune it a bit) would prevent any animation corruption due to view controller rearrangement. If you're not animating these changes, a direct function call should be sufficient.
Dismissing it if it's present is a proper way to achieve the functionality you want.
Upvotes: 2