当前视图控制器从AppDelegate?
有没有办法从AppDelegate获取当前的视图控制器? 我知道有rootViewController,但这不是我正在寻找。
如果你有UINavigationController到appDelegate,那么使用它的属性topViewController或visibleViewController
如果你的应用程序的根视图控制器是一个UINavigationController
你可以这样做:
((UINavigationController*)appDelegate.window.rootViewController).visibleViewController;
同样,如果它是一个UITabBarController
你可以这样做:
((UITabBarController*)appDelegate.window.rootViewController).selectedViewController;
当然,像这样明确的投射是肮脏的。 更好的办法是使用强types来捕获参考。
这可能有帮助
- (UIViewController *)topViewController{ return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; } - (UIViewController *)topViewController:(UIViewController *)rootViewController { if (rootViewController.presentedViewController == nil) { return rootViewController; } if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]) { UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController; UIViewController *lastViewController = [[navigationController viewControllers] lastObject]; return [self topViewController:lastViewController]; } UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController; return [self topViewController:presentedViewController]; }
Swift版本:
extension UIApplication { class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? { if let nav = base as? UINavigationController { return topViewController(base: nav.visibleViewController) } if let tab = base as? UITabBarController { if let selected = tab.selectedViewController { return topViewController(base: selected) } } if let presented = base?.presentedViewController { return topViewController(base: presented) } return base } }
取自: https : //gist.github.com/snikch/3661188
获取appDelegate对象:
MyAppDelegate *tmpDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
正如铍build议您可以使用UINavigationController的属性来访问您当前的视图控制器。
所以代码如下所示:
id myCurrentController = tmpDelegate.myNavigationController.topViewController;
要么:
NSArray *myCurrentViewControllers = tmpDelegate.myNavigationController.viewControllers;
进行扩展:
extension UIApplication { class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? { if let nav = base as? UINavigationController { return topViewController(nav.visibleViewController) } if let tab = base as? UITabBarController { let moreNavigationController = tab.moreNavigationController if let top = moreNavigationController.topViewController where top.view.window != nil { return topViewController(top) } else if let selected = tab.selectedViewController { return topViewController(selected) } } if let presented = base?.presentedViewController { return topViewController(presented) } return base } }
用法:
if let rootViewController = UIApplication.topViewController() { //do sth with root view controller }
你可以从rootViewController中获取当前的视图控制器,通过查找其presentViewController,像这样:
UIViewController *parentViewController = [[[UIApplication sharedApplication] delegate] window].rootViewController; while (parentViewController.presentedViewController != nil){ parentViewController = parentViewController.presentedViewController; } UIViewController *currentViewController = parentViewController;
它与我合作。 希望能帮助到你 :)
对于任何不使用UINavigationController
而是其默认视图控制器是UIViewController
,可以在AppDelegate
检查哪个视图控制器处于活动状态(或呈现状态)。
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int { if let rootViewController = self.window!.rootViewController { if let presentedViewController = rootViewController.presentedViewController { return presentedViewController.supportedInterfaceOrientations() } } // Else current view controller is DefaultViewController return Int(UIInterfaceOrientationMask.Portrait.rawValue) }
正如你所看到的,我正在检查当前的视图控制器,以支持特定视图控制器的不同的接口方向。 对于任何有兴趣使用这种方法来支持具体的以下内容的人,都应该放在每个需要特定方向的视图控制器中。
override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.All.rawValue) }
注意:这个代码是用Swift 1.2编写的
Swift解决scheme:
self.window.rootViewController.presentedViewController.
这应该得到你所需要的。
通常我需要检索当前显示的视图控制器。 这可能意味着当前UINavigationController,当前呈现的视图控制器等的堆栈顶部的视图控制器。所以我写了这个函数,大部分时间,它可以在UIViewController扩展中使用。
代码在Swift 3中 :
func currentViewController( _ viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? { guard let viewController = viewController else { return nil } if let viewController = viewController as? UINavigationController { if let viewController = viewController.visibleViewController { return currentViewController(viewController) } else { return currentViewController( viewController.topViewController) } } else if let viewController = viewController as? UITabBarController { if let viewControllers = viewController.viewControllers, viewControllers.count > 5, viewController.selectedIndex >= 4 { return currentViewController( viewController.moreNavigationController) } else { return currentViewController( viewController.selectedViewController) } } else if let viewController = viewController.presentedViewController { return viewController } else if viewController.childViewControllers.count > 0 { return viewController.childViewControllers[0] } else { return viewController } }
调用它: currentViewController()