In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how iOS gets the current controller. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Background
In the development process, it is often necessary to obtain the requirements of the current window, rootViewController, and currently displayed visibleController. If the .m implementation is not in the current view, we need to get the current controller quickly, then we need to do a layer of encapsulation first. I usually achieve it through a Category written by UIViewController, and it is very easy to implement. We only need to master a few methods of the controller.
Get the root controller
+ (UIViewController *) jsd_getRootViewController {UIWindow* window = [UIApplication sharedApplication] delegate] window]; NSAssert (window, @ "The window is empty"); return window.rootViewController;}
Here is very simple, through a single example to get the current UIApplication delegate, you can easily get the rootViewController through window.
Get the current page controller
+ (UIViewController*) jsd_findVisibleViewController {UIViewController* currentViewController = [self jsd_rootViewController]; BOOL runLoopFind = YES; while (runLoopFind) {if (currentViewController.presentedViewController) {currentViewController = currentViewController.presentedViewController;} else {if ([currentViewController isKindOfClass: [UINavigationController class]]) {currentViewController = (UINavigationController *) currentViewController) .visibleViewController;} else if ([currentViewController isKindOfClass: [UITabBarController class]]) {currentViewController = (UITabBarController*) currentViewController) .selectedViewController } else if ([currentViewController isKindOfClass: [UISplitViewController class]]) {/ / when Ipad compatibility is required, currentViewController = currentViewController.presentingViewController;} else {if (currentViewController.presentingViewController) {currentViewController = currentViewController.presentingViewController;} else {return currentViewController;} return currentViewController;}
Here, let's talk about the idea of implementation. If we want to get the current controller directly without coupling with the controller, we basically find it through rootViewController. After we get the rootViewControoler through the above method, we first look at presentedViewController, because the controller is presented in ways such as push and present. Let's first check whether it is from present. If so, we can find the current controller from present through this generic performance. Then check whether it belongs to UINavigationControler or UITabBarController, and if so, by looking for the top level of its sub-controller or the controller it is selecting. Finally, in the case of judging whether the current controller has a sub-controller, if so, take the top level of the sub-controller, otherwise the current controller is itself.
The main purpose here is to find the view controllers managed by the current application based on UITabBarController and UINavigationControler. If there are other controllers, you need to add if conditions to judge.
Method 2: when we have a view controller child View that is being rendered, we can find it recursively through the property nextResponder
+ (nullable UIViewController *) findBelongViewControllerForView: (UIView *) view {UIResponder * responder = view; while ((responder = [responder nextResponder])) if ([responder isKindOfClass: [UIViewController class]]) {return (UIViewController *) responder;} return nil;}
PresentedViewController
Apple document presentedViewControlle
Through this method, we can find the current controller introduced by presented mode (display and hermit). For example: AViewController-- > BViewController is introduced in modal mode. Then you can get the BViewController using AViewController.presentedViewController.
PresentingViewController
Apple document
Through this method, we can find the upper controller which derives the current controller through presented mode (display and hermit). For example: AViewController-- > BViewController is introduced in modal mode. Then you can get the AViewController using BViewController.presentingViewController.
ModalViewController
Check the document to find that this method has been abandoned after iOS 6, the official recommendation is to use presentedViewController instead.
Thank you for reading! This is the end of the article on "how to get the current controller for iOS". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.