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

不羁岁月 提交于 2019-12-11 10:02:57

问题


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.


回答1:


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)
        }



回答2:


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.




回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/7498880/how-to-determine-which-view-loads-on-returning-to-foreground-from-the-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!