Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use applicationDidBecomeActive in UIViewController?

I want to reload data in UIViewController when application become active or become foreground.

I know applicationDidBecomeActive is called in AppDelegate class.
But I have to have a global variable for the UIViewController to reload its data in AppDelegate class like this code:

in AppDelegate.m  // global variable UIViewController *viewController1; UIViewController *viewController2;  -(void)applicationDidBecomeActive:(UIApplication *)application {     [viewController1 reloadData];     [viewController2 reloadData]; } 

But it is inconvenient especially when I have a lot of UIViewControllers.

Can I use applicationDidBecomeActive in UIViewController instead of in AppDelegate class?
Or are there better ways than having global variable for UIViewController?

I also need to use the following method from UIViewControllers:

-(void)applicationWillResignActive:(UIApplication *)application -(void)applicationDidEnterBackground:(UIApplication *)application -(void)applicationWillEnterForeground:(UIApplication *)application 
like image 244
js_ Avatar asked Aug 13 '12 04:08

js_


People also ask

When Applicationwillenterforeground is called?

Tells the delegate that the app is about to enter the foreground.

What is UIApplication in Swift?

The application object maintains a list of open windows ( UIWindow objects), which it can use to retrieve any of the app's UIView objects. The UIApplication class defines a delegate that conforms to the UIApplicationDelegate protocol and must implement some of the protocol's methods.


1 Answers

At the time of reactivation, if you want to carry a particular thing for a view controller, you should register a notification in its viewDidLoad method.

UIApplicationDidBecomeActiveNotification will automatically notify your application and given controller, if they registered for it.

 [[NSNotificationCenter defaultCenter]addObserver:self                                          selector:@selector(yourMethod)                                              name:UIApplicationDidBecomeActiveNotification                                            object:nil]; 
like image 189
Apurv Avatar answered Sep 22 '22 09:09

Apurv