【问题标题】:Determine if instance of UIViewController subclass exists判断 UIViewController 子类的实例是否存在
【发布时间】:2023-03-28 10:05:01
【问题描述】:

如何检查继承 UIViewController 的所有活动对象的列表?

我想知道MyViewController 的实例是否存在。理想情况下,我可以在 UIApplicationDelegate 的回调中获取此信息(例如 application:didReceiveRemoteNotification:)。

我尝试过记录 navigationController.viewControllers 之类的内容,但没有运气。我还尝试了 navigationController 上的 topViewControllermodalViewController 属性。

【问题讨论】:

  • 这是出于调试目的还是您有其他想法? “存在”是指存在并且有有效的参考吗?还是你的意思是别的?
  • @Jim 用于调试。是的,我的意思是有一个有效的参考。

标签: objective-c ios logging uiviewcontroller uikit


【解决方案1】:

如果你知道你的 rootViewController 是 UINavigationController,你可以遍历视图控制器数组并测试它的类类型

BOOL success = NO;
NSArray *viewControllersArray = self.navigationController.viewControllers;
for (id vc in viewControllersArray)
{
    if ([vc isKindOfClass:[MyViewController class]])
        success = YES; // Found it!
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    相关资源
    最近更新 更多