Namratha
Namratha

Reputation: 16780

how to determine which view loads on returning to foreground from the background in iOS?

I have an application which has more than one view. When I return to the foreground from the background, I want to determine which view I am in presently. How would I do that?

[EDIT]

I understand visibleViewController property of navigationController might help. But, I want to know how to use the pointer returned by it in my comparison with a UIViewController object. How can we compare pointers? Some code snippets would be very helpful.

Upvotes: 1

Views: 5622

Answers (4)

Bobj-C
Bobj-C

Reputation: 5426

zirinisp's Answer in Swift:

extension UIWindow {
    func visibleViewController() -> UIViewController? {
        if let rootViewController: UIViewController  = self.rootViewController {
            return UIWindow.getVisibleViewControllerFrom(rootViewController)

    }
    return nil
}

class func getVisibleViewControllerFrom(vc:UIViewController) -> UIViewController {
if vc.isKindOfClass(UINavigationController.self) {

    let navigationController = vc as UINavigationController
    return UIWindow.getVisibleViewControllerFrom( navigationController.visibleViewController)

} else if vc.isKindOfClass(UITabBarController.self) {

    let tabBarController = vc as UITabBarController
    return UIWindow.getVisibleViewControllerFrom(tabBarController.selectedViewController!)

} else {

    if let presentedViewController = vc.presentedViewController {

        return UIWindow.getVisibleViewControllerFrom(presentedViewController)

    } else {

        return vc;
    }
}
}

Usage:

if let topController = window.visibleViewController() {
            println(topController)
        }

Upvotes: 3

zirinisp
zirinisp

Reputation: 10291

I always love solutions that involve categories as they are bolt on and can be easily reused.

So I created a category on UIWindow. You can now call visibleViewController on UIWindow and this will get you the visible view controller by searching down the controller hierarchy. This works if you are using navigation and/or tab bar controller. If you have another type of controller to suggest please let me know and I can add it.

UIWindow+PazLabs.h (header file)

#import <UIKit/UIKit.h>

@interface UIWindow (PazLabs)

- (UIViewController *) visibleViewController;

@end

UIWindow+PazLabs.m (implementation file)

#import "UIWindow+PazLabs.h"

@implementation UIWindow (PazLabs)

- (UIViewController *)visibleViewController {
    UIViewController *rootViewController = self.rootViewController;
    return [UIWindow getVisibleViewControllerFrom:rootViewController];
}

+ (UIViewController *) getVisibleViewControllerFrom:(UIViewController *) vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [UIWindow getVisibleViewControllerFrom:[((UINavigationController *) vc) visibleViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [UIWindow getVisibleViewControllerFrom:[((UITabBarController *) vc) selectedViewController]];
    } else {
        if (vc.presentedViewController) {
            return [UIWindow getVisibleViewControllerFrom:vc.presentedViewController];
        } else {
            return vc;
        }
    }
}

@end

Upvotes: 1

bandejapaisa
bandejapaisa

Reputation: 26952

You want to know when you app came into the foreground and became active. This needs to be somewhere at the root of your view controller hierarchy - possibly your AppDelegate, possibly your root view controller.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appActive:)
                                                 name:UIApplicationDidBecomeActiveNotification 
                                               object:nil];

In appActive:(NSNotification *)notification; or whatever method you call it - do what LucasTizma suggested.

Also remember to clean up your notifcations when you're done

 [[NSNotificationCenter defaultCenter] removeObserver:self];

Upvotes: 1

CIFilter
CIFilter

Reputation: 8677

In depends on what root view controller you're using. If you have a navigation controller, you can check the visibleViewController property to see which view controller is currently on top of the navigation stack. For a tab bar controller, check the selectedViewController property to see which tab is active. If it's a combination of the two, use both methods in tandem.

Each of the container view controllers provide some way or another to figure out what's selected or currently on screen. If you're doing your own special setup, you'll have to come up with a good way to determine this information on your own.

Upvotes: 2

Related Questions