简体   繁体   中英

Get top UIViewController from different UINavigationController without reference

I've a view hierarchy

a) UINavigationController > b) UIViewController > c) UITabbarController > d) UINavigationController

Ok, now at any point in app, I'm in b | c | d – I've added notifications to handle (and check) if a user goes (or comes) to background/foreground.

This works great, but if a user comes to foreground – I'm presenting a UIViewController (lets name it 'z') in a different (new) UINavigationController object.

This is also works great.. but heres' the problem if I'm already in z and user go background and the comes to foreground, I should not show z again as its already visible.

Problem I'm facing :

  • I don't keep reference of that different (new) UINavigationController object or even reference of z.

  • So – how do I catch that currently visible view is z itself?

If its from same navigation controller (like a) I may detect it by its property topViewController . But here I can't?

Any way to get this done wihtout creating reference for it?

You can present the controller(zController in your case) on window's root view controller and check if its already presented.

Present the controller on root view controller,

//Use this code to present the zController
    UIViewController *rootViewController =[[[[UIApplication sharedApplication] delegate] window] rootViewController];
    [rootViewController presentViewController:zController animated:YES completion:nil];

and when application enters foreground, you can check if the controller is already presented as follows,

- (BOOL)checkIfzControllerIsAlreadyPresented
{
    UIViewController *rootViewController =[[[[UIApplication sharedApplication] delegate] window] rootViewController];
    id obj = rootViewController.presentedViewController;
    if (obj && [obj isKindOfClass:[zController class]]) {
        return YES; //zController is already visble
    }
    return NO;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM