Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何知道应用程序启动时显示了哪个viewcontroller_Ios_Objective C_Viewcontroller - Fatal编程技术网

Ios 如何知道应用程序启动时显示了哪个viewcontroller

Ios 如何知道应用程序启动时显示了哪个viewcontroller,ios,objective-c,viewcontroller,Ios,Objective C,Viewcontroller,情况是: 应用程序在后台 用户点击应用程序图标 应用程序打开并显示上次应用程序进入后台之前的视图控制器 我想知道将要显示哪个视图控制器。我要找的东西是: - (void)applicationDidBecomeActive:(UIApplication *)application { if ([self.window.viewControllerOnScreen isKindOfClass:[HomeViewController class]]) { //do sth

情况是:

  • 应用程序在后台
  • 用户点击应用程序图标
  • 应用程序打开并显示上次应用程序进入后台之前的视图控制器
我想知道将要显示哪个视图控制器。我要找的东西是:

- (void)applicationDidBecomeActive:(UIApplication *)application {
    if ([self.window.viewControllerOnScreen isKindOfClass:[HomeViewController class]]) {
        //do sthg
    }
}
因为在这种情况下,是主视图控制器(嵌入到导航控制器中,我使用故事板),我会执行一些重新加载方法

[[self.navigationController viewControllers] lastObject];
第一部分将为您提供堆栈上所有ViewController的数组,最后一个对象是当前显示的对象。检查其类类型以查看是否为homeViewController

if (![[appDelegate.rootNavController topViewController] isMemberOfClass:NSClassFromString(@"LGChatViewController")]) {}

第一部分将为您提供堆栈上所有ViewController的数组,最后一个对象是当前显示的对象。检查其类类型以查看是否为homeViewController尝试此操作

 - (UIViewController *)topViewController{
         return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
 }

     - (UIViewController *)topViewController:(UIViewController *)rootViewController
    {
     if (rootViewController.presentedViewController == nil) {
     return rootViewController;
     }

     if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
          UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
          UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
     return [self topViewController:lastViewController];
     }

      UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
     return [self topViewController:presentedViewController];
    }
if (![[appDelegate.rootNavController topViewController] isMemberOfClass:NSClassFromString(@"LGChatViewController")]) {}

试试这个

 - (UIViewController *)topViewController{
         return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
 }

     - (UIViewController *)topViewController:(UIViewController *)rootViewController
    {
     if (rootViewController.presentedViewController == nil) {
     return rootViewController;
     }

     if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
          UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
          UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
     return [self topViewController:lastViewController];
     }

      UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
     return [self topViewController:presentedViewController];
    }
如果您使用选项卡栏,那么您可以这样设置,并显示第一个帐户,然后继续

如果您使用选项卡栏,那么您可以这样设置,并显示第一个帐户并继续……

根据此设置,当应用程序进入后台时,每个对象都会收到
UIApplicationIdentinterBackgroundNotification
通知。类似地,当应用程序出现在前台时,
UIApplicationWillEnterForegroundNotification
将被触发

所以,当应用程序进入前台时,你们可以用它来跟踪哪个视图控制器被打开

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(appEnteredForeground:)
                                             name:UIApplicationDidEnterForegroundNotification
                                           object:nil];
因此,当应用程序进入后台时,每个对象都会收到
UIApplicationIdentinterBackgroundNotification
通知。类似地,当应用程序出现在前台时,
UIApplicationWillEnterForegroundNotification
将被触发

所以,当应用程序进入前台时,你们可以用它来跟踪哪个视图控制器被打开

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(appEnteredForeground:)
                                             name:UIApplicationDidEnterForegroundNotification
                                           object:nil];

我这样做是为了获取当前的viewController

if (![[appDelegate.rootNavController topViewController] isMemberOfClass:NSClassFromString(@"LGChatViewController")]) {}

我这样做是为了获取当前的viewController

if (![[appDelegate.rootNavController topViewController] isMemberOfClass:NSClassFromString(@"LGChatViewController")]) {}

这丝毫没有回答用户的问题,而是问发生了什么。。。在回答第一个问题之前,请仔细阅读问题。这绝对不能回答用户的问题。请询问发生了什么。。。在回答第一个问题之前,请仔细阅读问题您是否建议在每个viewController中使用此选项以有效复制导航栏的viewController属性??如果是这样的话,这是一个可怕的错误!ideanot不在每个viewcontroller中,仅在必须重新加载数据的homeviewController中。查看此链接您是否建议在每个viewController中使用此选项以有效复制导航栏的viewController属性??如果是这样的话,这是一个可怕的错误!ideanot不在每个viewcontroller中,仅在必须重新加载数据的homeviewController中。查看此链接不要忘记标记最重要的答案,并对帮助您的答案进行投票。其他面临同样问题的人会想知道是什么解决了你的问题,而那些花时间回答问题的人应该得到代表分数。如果没有人回答你的问题,请留下评论并提供更多信息。别忘了标记最重要的答案,并投票选出对你有帮助的答案。其他面临同样问题的人会想知道是什么解决了你的问题,而那些花时间回答问题的人应该得到代表分数。如果没有人回答你的问题,请留下评论并提供更多信息