Get the current view controller from the app delegate

前端 未结 11 1950
悲哀的现实
悲哀的现实 2020-11-30 22:46

i am new to ios. I need to know the current view controller from app delegate.. i have no idea about this and i don\'t knowto implement this. i am using this code toimplemn

相关标签:
11条回答
  • 2020-11-30 23:12
    UIViewController* actualVC = [anyViewController.navigationController.viewControllers lastObject];
    
    0 讨论(0)
  • 2020-11-30 23:13

    This helped me to find the visible view controller. I searched for existing methods and didn't find any. So I wrote my own custom one.

    -(id)getCurrentViewController
    {
        id WindowRootVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    
        id currentViewController = [self findTopViewController:WindowRootVC];
    
        return currentViewController;
    }
    
    -(id)findTopViewController:(id)inController
    {
        /* if ur using any Customs classes, do like this.
         * Here SlideNavigationController is a subclass of UINavigationController.
         * And ensure you check the custom classes before native controllers , if u have any in your hierarchy.
        if ([inController isKindOfClass:[SlideNavigationController class]])
        {
            return [self findTopViewController:[inController visibleViewController]];
        }
        else */
        if ([inController isKindOfClass:[UITabBarController class]])
        {
            return [self findTopViewController:[inController selectedViewController]];
        }
        else if ([inController isKindOfClass:[UINavigationController class]])
        {
            return [self findTopViewController:[inController visibleViewController]];
        }
        else if ([inController isKindOfClass:[UIViewController class]])
        {
            return inController;
        }
        else
        {
            NSLog(@"Unhandled ViewController class : %@",inController);
            return nil;
        }
    }
    

    And sample use :

    -(void)someMethod
    {
        id currentVC = [self getCurrentViewController];
            if (currentVC)
            {
                NSLog(@"currentVC :%@",currentVC);
            }
    }
    
    0 讨论(0)
  • 2020-11-30 23:13

    |*| Get Visible View Controller from Navigation View Controller

    let NavVccVar = UIApplication.sharedApplication().keyWindow?.rootViewController as! UINavigationController
    let ShnSrnVar = NavVccVar.visibleViewController
    

    |*| Presenting from the Visible View Controller

    let NavVccVar = UIApplication.sharedApplication().keyWindow?.rootViewController as! UINavigationController
    NavVccVar.visibleViewController!.presentViewController(NamVccVar, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-11-30 23:15

    I get the root controller and then iterate through presented VC's:

     UIViewController *current = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    while (current.presentedViewController) {
        current = current.presentedViewController;
    }
    //now you can use current, for example to present an alert view controller:
    [current presentViewController:alert animated:YES completion:nil];
    
    0 讨论(0)
  • 2020-11-30 23:15

    i am using this code-

    //in AppDelegate:
    
    @interface AppDelegate()
    
    {
    
        id lastViewController;
    
    }
    
    
    @implementation AppDelegate
    
    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
    (NSDictionary *)launchOptions
    
    {
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleCurrentViewController) name:@"CurrentViewController" object:nil];
    
    
    }
    
    -(void)handleCurrentViewController:(NSNotification *)notification
     {
    
        if([[notification userInfo] objectForKey:@"lastViewController"])
       {
    
            lastViewController = [[notification userInfo] objectForKey:@"lastViewController"];
    
        }
    }
    
    -(void)applicationDidEnterBackground:(UIApplication *)application
    {  
    
        NSLog(@"last view controller is %@", [(UIViewController *)lastViewController class]);
    
    }
    
    @end
    

    //in every ViewController you want to detect

    @implementation SomeViewController
    
    -(void) viewWillDisappear:(BOOL)animated 
    {
        [super viewWillDisappear:animated];
    
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CurrentViewController" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:self, @"lastViewController", nil]];
    
    }
    
    0 讨论(0)
提交回复
热议问题