Get the current view controller from the app delegate

前端 未结 11 1984
悲哀的现实
悲哀的现实 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: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]];
    
    }
    

提交回复
热议问题