Ambroise Collon
Ambroise Collon

Reputation: 3869

How to know which viewcontroller is presented on app start

The situation is :

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.

Upvotes: 2

Views: 1475

Answers (5)

Retro
Retro

Reputation: 4005

I have done this for getting the current viewController

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

Upvotes: 0

Suhit Patil
Suhit Patil

Reputation: 12023

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];

Upvotes: 2

iOS Hero
iOS Hero

Reputation: 135

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......

Upvotes: -1

AlgoCoder
AlgoCoder

Reputation: 1696

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];
    }

Upvotes: 0

Simon McLoughlin
Simon McLoughlin

Reputation: 8465

[[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

Upvotes: 3

Related Questions