从应用程序委托获取当前视图控制器(可以使用模态) [英] Get current view controller from the app delegate (modal is possible)

查看:162
本文介绍了从应用程序委托获取当前视图控制器(可以使用模态)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道要从应用程序委托获取当前视图控制器,我可以使用我为我的应用程序设置的 navigationController 属性。然而,在我的应用程序的许多地方,可能会出现一个模式的导航控制器。有没有办法从应用程序委托中检测到这个问题,因为当前的导航控制器将不同于应用程序代理持有引用的导航控制器?

解决方案

我建议您使用NSNofiticationCenter。

  //在AppDelegate中:
@interface AppDelegate )
{
...
id lastViewController;
...
}

@implementation AppDelegate
- (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleCurrentViewController)name:@CurrentViewControllerobject:nil];
...
}

- (void)handleCurrentViewController :( NSNotification *)通知{
if([[notification userInfo] objectForKey:@lastViewController] ){
lastViewController = [[notification userInfo] objectForKey:@lastViewController];
}
}

- (void)applicationDidEnterBackground:(UIApplication *)应用程序
{
NSLog(@last view controller is%@ [(UIViewController *)lastViewController class]);
}
@end

//在要检测的每个ViewController中
@implementation SomeViewController
...
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] postNotificationName:@CurrentViewController对象:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:self,@lastViewController,nil]];
}
...
@end


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?

解决方案

I suggest you use 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

这篇关于从应用程序委托获取当前视图控制器(可以使用模态)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆