ios - 查找当前事件的 View Controller

标签 ios uiviewcontroller uistoryboard appdelegate

我有一个登录 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/

相关文章:

ios - 如何将标题/按钮添加到 UIViewController 内添加的 UINavigationController?

Swift:如何在显示之前获取 subview 的大小?

ios - 具有默认 nil 值的 UIDatePicker - iOS

ios - didSelectRowAt 未被调用,但委托(delegate)和数据源已设置

ios - 如何在 drawRect 中设置圆角半径

ios - 使用 Storyboard在导航 Controller 中设置 UITableView

ios - 分段控制位置随 Storyboard变化

ios - 带有复选框的交互式 TableView 标题

ios - NSURLSession:后台上传然后调用服务api

ios - 为什么 Apple Push Notification Authentication Key (Sandbox & Production) 没有出现