背景
在開發(fā)過程中,經(jīng)常需要獲取當(dāng)前 window, rootViewController, 以及當(dāng)前 ViewController 的需求. 如果 .m 實(shí)現(xiàn)不是在當(dāng)前視圖情況下, 我們需要快速的獲取到當(dāng)前控制器, 這種情況就需要先做好一層封裝,我一般是通過 UIViewController 寫的一個(gè) Category 來實(shí)現(xiàn), 實(shí)現(xiàn)起來也非常簡(jiǎn)單, 只需要我們對(duì) 控制器幾個(gè)方法掌握便可。
獲取根控制器
+ (UIViewController *)jsd_getRootViewController{ UIWindow* window = [[[UIApplication sharedApplication] delegate] window]; NSAssert(window, @"The window is empty"); return window.rootViewController;}
這里很簡(jiǎn)單, 通過單例獲取到當(dāng)前 UIApplication 的 delegate 在通過 window 即可輕松拿到 rootViewController。
獲取當(dāng)前頁(yè)面控制器
+ (UIViewController *)jsd_getCurrentViewController{ UIViewController* currentViewController = [self jsd_getRootViewController]; BOOL runLoopFind = YES; while (runLoopFind) { if (currentViewController.presentedViewController) { currentViewController = currentViewController.presentedViewController; } else if ([currentViewController isKindOfClass:[UINavigationController class]]) { UINavigationController* navigationController = (UINavigationController* )currentViewController; currentViewController = [navigationController.childViewControllers lastObject]; } else if ([currentViewController isKindOfClass:[UITabBarController class]]) { UITabBarController* tabBarController = (UITabBarController* )currentViewController; currentViewController = tabBarController.selectedViewController; } else { NSUInteger childViewControllerCount = currentViewController.childViewControllers.count; if (childViewControllerCount > 0) { currentViewController = currentViewController.childViewControllers.lastObject; return currentViewController; } else { return currentViewController; } } } return currentViewController;}
這里講一下實(shí)現(xiàn)思路, 我們想要與控制器無耦合的情況下, 想要直接獲取到當(dāng)前控制器, 基本都是通過 rootViewController 來查找的, 通過上面的方法拿到 rootViewControoler 之后, 我們先看 presentedViewController, 因?yàn)榭刂破鞒尸F(xiàn)出來的方式有 push 與 present, 我們先查看它是否是 present 出來的, 如果是則通過此屬性能找到 present 出來的當(dāng)前控制器, 然后在檢查是否屬于 UINavigationControler 或 UITabBarController ,如果是則通過查找其子控制器里面最頂層或者其正在選擇的控制器。
最后在判斷當(dāng)前控制器是否有子控制器的情況, 如果有則取其子控制器最頂層, 否則當(dāng)前控制器就是其本身。
這里主要是查找當(dāng)前 應(yīng)用程序基于 UITabBarController 和 UINavigationControler 下管理的視圖控制器, 如果還有其他控制器則需要添加 if 條件來進(jìn)行判斷。
presentedViewController
Apple 文檔 presentedViewControlle
通過此方法可以查找到通過 presented 模態(tài)方式(顯示與隱士) 方式推出的當(dāng)前控制器。
例如: AViewController --> BViewController 通過模態(tài)方式推出.
則使用 AViewController.presentedViewController 能獲取到 BViewController。
presentingViewController
通過此方法可以查找到通過 presented 模態(tài)方式(顯示與隱士) 方式推出當(dāng)前控制器的上層控制器。
例如: AViewController --> BViewController 通過模態(tài)方式推出.
則使用 BViewController.presentingViewController 能獲取到 AViewController。
modalViewController
查看文檔發(fā)現(xiàn)此方法已在 iOS 6 后被棄用, 官方推薦直接使用 presentedViewController 替代即可.
PS:通過View獲取View所在的控制器
+(UIViewController*)getViewcontrollerView:(UIView*)view{ UIViewController *vc = nil; for (UIView *temp = view; temp;temp = temp.superview) { if ([temp.nextResponder isKindOfClass:[UIViewController class]]) { vc = (UIViewController*)temp.nextResponder; break; } } return vc;}
通過遞歸方法遍歷當(dāng)前View的所有子試圖
+(void)getMysubViewsWithViews:(UIView *)view{ NSArray *arrayViews = view.subviews; for (UIView * obj in arrayViews) { if ([obj isKindOfClass:[MyView class]]) { NSLog(@"找到了 %@",MyView); } [self getMysubViewsWithViews:obj]; }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注