How to tell the active view controller when applicationDidBecomeActive is called?

后端 未结 6 1290
孤独总比滥情好
孤独总比滥情好 2021-02-02 12:01

I feel I am missing a trick here...

I just want to call viewDidLoad or viewDidAppear on the current active view controller when applicationDidBecomeActive gets called, s

6条回答
  •  执念已碎
    2021-02-02 12:56

    your AppDelegate will have a window property, that window will have a rootViewController property. You can find your viewController here.

    If you are using a TabBarController, the rootviewcontroller will be the tabbarcontroller, and you can call the tabbarcontroller's selectedViewController to get the current viewController.

    UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    if ([rootViewController isKindOfClass:[UITabBarController Class]])
        rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
    else if ([rootViewController isKindOfClass:[UINavigationController Class]])
        rootViewController = ((UINavigationController *)rootViewController).topViewController;
    
    [rootViewController viewDidAppear];
    

    If you have a more complex view hierarchy with navigation controllers, or modal views, you can call on presentedViewController, or topViewController.

提交回复
热议问题