在applicationDidEnterBackground中保存UITableView属性并在iOS中加载它applicationDidBecomeActive

时间:2014-11-11 13:15:28

标签: ios objective-c uiviewcontroller uiapplication

在我的iOS应用程序中,我在UIViewController中有一个UITableView。数据加载完成到UITableView后,当我按下iPhone的Home键时,应用程序将进入后台。它将执行以下方法。

- (void)applicationDidEnterBackground:(UIApplication *)application {

当我点击应用程序图标并启动应用程序时,它将在AppDelegate中调用以下方法

- (void)applicationWillEnterForeground:(UIApplication *)application {

- (void)applicationDidBecomeActive:(UIApplication *)application {

但没有UIViewController方法。因此我所做的是,被称为自己创建的自定义方法,它位于我的UIViewController类中,如下所示。此代码位于 applicationDidBecomeActive 方法内。

MyViewController *tViewCont = [MyViewController alloc];
[tViewCont remindToPopulate];

我输入了一条日志消息并确认正在执行 remindToPopulate 方法。在该方法中,我想重新加载UITableView。

但是此时我声明的UITableView属性设置为nil。保存UITableView属性并使用最新数据加载它的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

为此,您可以在控制器的viewDidLoad中添加UIApplicationDidBecomeActiveNotificationUIApplicationWillEnterForegroundNotification的通知。

只需添加

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(yourmethod) name:UIApplicationDidBecomeActiveNotification object:nil];

和你的方法

-(void)yourmethod{
 [yourtableview reloadData];
}

您还可以添加UIApplicationWillEnterForegroundNotification通知。根据您的需要添加。