Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

iphone - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...