2013-04-08 134 views
0

我有一个登录故事板,我在用户登录时实例化。我有一个主要故事板,它是实际的应用程序。查找当前视图控制器处于活动状态

当我的应用程序设置为非活动状态(关闭应用程序),然后重新激活(再次打开应用程序)时,AppDelegate会检查2分钟超时是否已经发生。如果是这样,我想显示一个警告,说它已经超时,而且这个工作很好。

我遇到的问题是,如果你在登录屏幕上,我不想显示消息。由于我的故事板使用TabBarController,我没有有效的导航控制器。如何确定LoginViewController是否正在从App Delegate中显示?我如何获得最顶级的视图控制器的类名?

NavigationController为null,仅供参考

回答

1

首先,你需要有对的UITabBarController一个参考。如果它在IB中设置为初始视图控制器,这非常简单。你可以通过打开故事板并在你的UITabBarController左边寻找一个灰色的小箭头来检查。如果是这样,那么简单地做到这一点:

UITabBarController *myTabBarController; 
if ([_window.rootViewController isKindOfClass:[UITabBarController class]]) { 

    NSLog(@"This window's rootViewController is of the UITabBarController class"); 

    myTabBarController = (UITabBarController *)_window.rootViewController; 

} 

如果您正在使用的UITabBarController,你就可以用其子UIViewControllers引用:

[myTabBarController objectAtIndex:index]; 

您还可以直接查询您的TabBarController:

NSLog(@"Selected view controller class type: %@, selected index: %d", [myTabBarController selectedViewController], [myTabBarController selectedIndex]); 

基于零的索引方案按照您设置的标签顺序,无论是编程方式还是通过IB(最左边的标签=索引0)。

一旦你有关于你的UITabBarController,其余的是很简单:

LoginViewController* myLoginViewController; 

if(![[myTabBarController selectedViewController] isKindOfClass:[LoginViewController class]){ 
    //If the currently selected ViewController is NOT the login page 
    //Show timeout alert 
} 
+0

我结束了有错误我感动的viewDidAppear登录后显示做同样的事情,而它在的appDelegate工作每隔一段时间在测试(发布)中它都会失败。希望我会首先看到这一点,但答案非常好。然而,发生了什么,我有一个独立的登录故事板和实际应用程序的主要故事板。感谢您的伟大答案... – 2013-11-26 19:32:25

相关问题