I know that to get the current view controller from the app delegate, I can use the navigationController
property I have set up for my app. However, it's possible in many places throughout my app that a modal navigation controller could have been presented. Is there any way to detect this from the app delegate, since the current navigation controller will be different from the one to which the app delegate holds a reference?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
- How can I add media attachments to my push notific
Great solution in Swift, implement in AppDelegate
Objective - C
You could try getting the top most view by doing:
although this view might be invisible or even covered by some of its subviews...
It depends on your UI but it might help.
I suggest you use NSNofiticationCenter.
Other solutions above work only partially as complex view hierarchies are not handled (navigation controller integrated in tab bar controller, split view controllers, view containers and also alert controllers could mess things up).
I know the question was about Objective-C, but I can provide only Swift code and I'm sure it will be useful to both types of users.
First: I've implemented a protocol to keep things clean and reusable:
Second: I've added a reference to AppDelegate:
Third: I set the current view controller in viewDidAppear():
Fourth:
Based on the gist here, I made a category to obtain the top most view controller, such that calling
[[UIApplication sharedApplication] topMostViewController]
will give you the top most view controller in your app.This is especially useful in iOS 8 where
UIAlertView
andUIActionSheet
have been deprecated in favor ofUIAlertController
, which needs to be presented on the top most view controller.UIViewController+TopMostViewController.h
UIViewController+TopMostViewController.m
Below code works very well.
+(UIViewController*) findBestViewController:(UIViewController*)vc {
}
+(UIViewController*) currentViewController {
}