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

后端 未结 6 1353
孤独总比滥情好
孤独总比滥情好 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:48

    I would recommend using notifications.

    In your app delegate's applicationdidBecomeActive method put in this code:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"appDidBecomeActive" object:nil];
    

    In your current active view controller's init method subscribe to the notification.

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(updateStuff)        
                                                 name:@"appDidBecomeActive" 
                                               object:nil];
    

    Implement the "updateStuff" method in your controller and you should be able to do whatever you want when the app becomes active.

提交回复
热议问题