2

The situation is :

  • App is on background
  • The user click on icon app
  • App open and show the view controller where we were before apps entered background last time.

I'd like to know which view controller is about to be presented. I'm looking for something like :

- (void)applicationDidBecomeActive:(UIApplication *)application {
    if ([self.window.viewControllerOnScreen isKindOfClass:[HomeViewController class]]) {
        //do sthg
    }
}

Because in case, it's the home view controller (embed in a navigation controller and i use storyboards) i would perform some reload method.

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
Ambroise Collon
  • 3,839
  • 3
  • 18
  • 37
  • don't forget to mark top answer and upvote the answer(s) that helped you. Others facing the same issue will want to know what fixed your issue and the people who put time into answering a question deserve the rep points. If none of them answered your question, leave comments and give more info – Simon McLoughlin Jan 07 '14 at 12:52

5 Answers5

3
[[self.navigationController viewControllers] lastObject];

The first part will give you an array of all of the viewControllers on the stack, with the last object being the one that is currently display. Check its class type to see is it the homeViewController

Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
2

As per this link Each object receive a UIApplicationDidEnterBackgroundNotification notification when the app goes in background. Similarly UIApplicationWillEnterForegroundNotification gets fired when app comes in foreground.

so you can use it to keep track of which view controller is opened when app enters foreground

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(appEnteredForeground:)
                                             name:UIApplicationDidEnterForegroundNotification
                                           object:nil];
Community
  • 1
  • 1
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
  • Are you proposing using this in every viewController to effectively duplicate the navigation bar's viewControllers property ?? if so this is a terrible! idea – Simon McLoughlin Jan 07 '14 at 12:49
  • not in every viewcontroller, only in homeviewController where the data has to be reloaded. see this link http://stackoverflow.com/questions/15971659/when-my-app-enters-foreground-view-is-not-reloaded-with-refresh-data – Suhit Patil Jan 07 '14 at 13:00
0

Try this

 - (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];
    }
AlgoCoder
  • 1,706
  • 5
  • 19
  • 40
0

I have done this for getting the current viewController

if (![[appDelegate.rootNavController topViewController] isMemberOfClass:NSClassFromString(@"LGChatViewController")]) {}
Retro
  • 3,985
  • 2
  • 17
  • 41
-1
self.tabBarController.viewControllers = [NSArray arrayWithObjects: [self LoadAccount], [self LoadContacts], [self LoadPhoneLine], [self LoadSettings], nil];

if you use tab bar then you set like this and show first account and go on......

iOS Hero
  • 135
  • 10