我有一个登录 Storyboard,每当用户登录时我都会实例化它。我有一个主 Storyboard,它是实际的应用程序。
当我的应用程序设置为非事件状态(关闭应用程序)然后重新激活(再次打开应用程序)时,AppDelegate 将检查是否发生了 2 分钟超时。如果是这样,我想显示一个警报,表明它已超时,这效果很好。
我遇到的问题是,如果您在登录屏幕上,我不想显示该消息。由于我的 Storyboard 使用 TabBarController,因此我没有有效的导航 Controller 。如何确定 LoginViewController 当前是否正在从 App Delegate 中显示?如何获取最顶层 View Controller 的类名?
NavigationController 为空,仅供引用
最佳答案
首先,您需要拥有对 UITabBarController 的引用。如果将其设置为 IB 中的初始 View Controller ,则这非常容易。您可以通过打开 Storyboard并在 UITabBarController 左侧查找一个灰色的小箭头来检查这一点。如果是这样,那么只需执行以下操作:
UITabBarController *myTabBarController;
if ([_window.rootViewController isKindOfClass:[UITabBarController class]]) {
NSLog(@"This window's rootViewController is of the UITabBarController class");
myTabBarController = (UITabBarController *)_window.rootViewController;
}
如果您使用 UITabBarController,您可以通过以下方式获取对其子 UIViewController 的引用:
[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
}
关于ios - 查找当前事件的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15882472/