如何在UIViewController中使用applicationDidBecomeActive? [英] How can I use applicationDidBecomeActive in UIViewController?

查看:129
本文介绍了如何在UIViewController中使用applicationDidBecomeActive?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用程序变为活动状态或成为前台时,我想在UIViewController中重新加载数据。

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

我知道在AppDelegate类中调用applicationDidBecomeActive。

但是我必须有一个UIViewController的全局变量来重新加载AppDelegate类中的数据,如下代码:

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

但是当我有很多UIViewControllers时它很不方便。

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

我可以在UIViewController中而不是在AppDelegate类中使用applicationDidBecomeActive吗?

或者有更好的方法而不是UIViewController的全局变量?

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

我还需要使用UIViewControllers中的以下方法:

I also need to use the following method from UIViewControllers:

-(void)applicationWillResignActive:(UIApplication *)application
-(void)applicationDidEnterBackground:(UIApplication *)application
-(void)applicationWillEnterForeground:(UIApplication *)application


推荐答案

在重新激活时,如果你想为视图控制器携带一个特定的东西,你应该在其 viewDidLoad 方法。

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 会自动通知您的申请和给定控制器,若是ey注册了它。

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

这篇关于如何在UIViewController中使用applicationDidBecomeActive?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆