我知道要从app委托获取当前视图控制器,我可以使用我为我的应用设置的navigationController
属性。但是,在我的应用程序的许多地方都可以提供模态导航控制器。有没有办法从app委托中检测到这一点,因为当前的导航控制器将与app delegate持有引用的导航控制器不同?
答案 0 :(得分:9)
基于gist here,我创建了一个类别来获取最顶层的视图控制器,这样调用[[UIApplication sharedApplication] topMostViewController]
将为您提供应用中最顶层的视图控制器。
这在iOS 8中特别有用,其中UIAlertView
和UIActionSheet
已被弃用,而UIAlertController
则需要在最顶层的视图控制器上显示。
#import <UIKit/UIKit.h>
@interface UIViewController (TopMostViewController)
- (UIViewController *)topMostViewController;
@end
@interface UIApplication (TopMostViewController)
- (UIViewController *)topMostViewController;
@end
#import "UIViewController+TopMostViewController.h"
@implementation UIViewController (TopMostViewController)
- (UIViewController *)topMostViewController
{
if (self.presentedViewController == nil)
{
return self;
}
else if ([self.presentedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navigationController = (UINavigationController *)self.presentedViewController;
UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
return [lastViewController topMostViewController];
}
UIViewController *presentedViewController = (UIViewController *)self.presentedViewController;
return [presentedViewController topMostViewController];
}
@end
#pragma mark -
@implementation UIApplication (TopMostViewController)
- (UIViewController *)topMostViewController
{
return [self.keyWindow.rootViewController topMostViewController];
}
@end
答案 1 :(得分:7)
我建议您使用NSNofiticationCenter。
//in AppDelegate:
@interface AppDelegate()
{
...
id lastViewController;
...
}
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleCurrentViewController) name:@"CurrentViewController" object:nil];
...
}
- (void)handleCurrentViewController:(NSNotification *)notification {
if([[notification userInfo] objectForKey:@"lastViewController"]) {
lastViewController = [[notification userInfo] objectForKey:@"lastViewController"];
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"last view controller is %@", [(UIViewController *)lastViewController class]);
}
@end
//in every ViewController you want to detect
@implementation SomeViewController
...
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CurrentViewController" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:self, @"lastViewController", nil]];
}
...
@end
答案 2 :(得分:4)
这对我有用。我有很多具有不同控制器的目标,所以以前的答案似乎没有用。
首先你要在AppDelegate类中使用它:
var window: UIWindow?
然后,在你的函数中
let navigationController = window?.rootViewController as? UINavigationController
if let activeController = navigationController!.visibleViewController {
if activeController.isKindOfClass( MyViewController ) {
println("I have found my controller!")
}
}
答案 3 :(得分:3)
您可以尝试获取最顶层的视图:
[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
虽然这个视图可能看不见甚至被其某些子视图覆盖......
这取决于您的用户界面,但它可能会有所帮助。
答案 4 :(得分:3)
Swift的绝佳解决方案,在AppDelegate中实现
func getTopViewController()->UIViewController{
return topViewControllerWithRootViewController(UIApplication.sharedApplication().keyWindow!.rootViewController!)
}
func topViewControllerWithRootViewController(rootViewController:UIViewController)->UIViewController{
if rootViewController is UITabBarController{
let tabBarController = rootViewController as! UITabBarController
return topViewControllerWithRootViewController(tabBarController.selectedViewController!)
}
if rootViewController is UINavigationController{
let navBarController = rootViewController as! UINavigationController
return topViewControllerWithRootViewController(navBarController.visibleViewController)
}
if let presentedViewController = rootViewController.presentedViewController {
return topViewControllerWithRootViewController(presentedViewController)
}
return rootViewController
}
目标 - C
- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
答案 5 :(得分:1)
如果App Delegate中有导航控制器,只需使用visibleViewController属性即可。它会为你提供可见的控制器,即使它是一个模态。
答案 6 :(得分:1)
在swift中你可以得到这样的活动ViewController:
let navigationController = application.windows[0].rootViewController as UINavigationController
let activeViewCont = navigationController.visibleViewController
答案 7 :(得分:0)
最佳解决方案也适用于moreNavigationController
中的UITabBarController
:
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
}
}
答案 8 :(得分:0)
下面的代码效果非常好。
+(UIViewController *)findBestViewController:(UIViewController *)vc {
if (vc.presentedViewController) {
// Return presented view controller
return [AppDelegate findBestViewController:vc.presentedViewController];
} else if ([vc isKindOfClass:[UISplitViewController class]]) {
// Return right hand side
UISplitViewController* svc = (UISplitViewController*) vc;
if (svc.viewControllers.count > 0)
return [AppDelegate findBestViewController:svc.viewControllers.lastObject];
else
return vc;
} else if ([vc isKindOfClass:[UINavigationController class]]) {
// Return top view
UINavigationController* svc = (UINavigationController*) vc;
if (svc.viewControllers.count > 0)
return [AppDelegate findBestViewController:svc.topViewController];
else
return vc;
} else if ([vc isKindOfClass:[UITabBarController class]]) {
// Return visible view
UITabBarController* svc = (UITabBarController*) vc;
if (svc.viewControllers.count > 0)
return [AppDelegate findBestViewController:svc.selectedViewController];
else
return vc;
} else {
// Unknown view controller type, return last child view controller
return vc;
}
}
+(UIViewController *)currentViewController {
// Find best view controller
UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
return [AppDelegate findBestViewController:viewController];
}
答案 9 :(得分:0)
上述其他解决方案仅部分工作,因为未处理复杂的视图层次结构(集成在标签栏控制器,拆分视图控制器,视图容器以及警报控制器中的导航控制器可能会搞乱)。
我通过保留当前视图控制器的引用来解决这个问题 AppDelegate中。每次视图出现时我都会利用
viewDidAppear(animated:)
并在应用委托中设置引用。
我知道问题是关于Objective-C,但我只提供Swift代码,我相信它对两类用户都有用。
首先:我已经实施了一个协议来保持清洁和可重复使用:
protocol UpdatableViewController {
func updateUI()
}
第二:我添加了对AppDelegate的引用:
var currentViewController: UpdatableViewController?
第三:我在viewDidAppear()中设置当前视图控制器:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
appDelegate?.currentViewController = self
}
第四:
extension ViewController1: UpdatableViewController {
func updateUI() {
print("Implement updating here")
}
}