在ApplicationIdentinterBackground时保存UITableView属性,并在iOS中将其加载回ApplicationIDBecMeactive
在我的iOS应用程序中,我的UIViewController中有一个UITableView。将数据加载到UITableView后,当我按下iPhone的Home按钮时,应用程序将进入后台。它将执行以下方法在ApplicationIdentinterBackground时保存UITableView属性,并在iOS中将其加载回ApplicationIDBecMeactive,ios,objective-c,uiviewcontroller,uiapplication,Ios,Objective C,Uiviewcontroller,Uiapplication,在我的iOS应用程序中,我的UIViewController中有一个UITableView。将数据加载到UITableView后,当我按下iPhone的Home按钮时,应用程序将进入后台。它将执行以下方法 - (void)applicationDidEnterBackground:(UIApplication *)application { 当我点击应用程序图标并启动应用程序时,它将在AppDelegate中调用以下方法 - (void)applicationWillEnterForegrou
- (void)applicationDidEnterBackground:(UIApplication *)application {
当我点击应用程序图标并启动应用程序时,它将在AppDelegate中调用以下方法
- (void)applicationWillEnterForeground:(UIApplication *)application {
及
但是没有一种方法可以使用。因此,我所做的是,调用一个由我自己创建的自定义方法,它位于我的UIViewController类中,如下所示。此代码位于ApplicationIDBecMeactive方法内部
我放置了一条日志消息,并确认正在执行提醒填充方法。在该方法中,我希望重新加载UITableView
但此时,我声明的UITableView属性被设置为nil。保存UITableView属性并将其加载回最新数据的正确方法是什么?为此,您可以在控制器的viewDidLoad中为
UIApplicationIDBecMeactiveNotification
或UIApplicationWillEnterForegroundNotification
添加通知
加上
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(yourmethod) name:UIApplicationDidBecomeActiveNotification object:nil];
在你的方法中
-(void)yourmethod{
[yourtableview reloadData];
}
您还可以添加
UIApplicationWillEnterForegroundNotification
通知。根据需要添加。添加[[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(yourmethod)名称:UIApplicationIDBecMeactiveNotification对象:nil]代码>在viewDidLoad
中。当您的应用程序处于活动状态时,它将触发。您还可以添加UIApplicationWillEnterForegroundNotification
。根据需要添加您是否使用topviewcontroller而不是alloc进行了检查?@Chinttu Maddy Ramani谢谢。似乎您的答案是正确的。@ismail您的意思是UIViewController*root=\u window.rootViewController代码>代码?请解释一下你在说什么。然后我可以检查。如果您使用的是导航控制器,那么我的意思是像这样的MyViewController*tViewCont=self.navigationcontroller.rootviewcontroller;是否有任何副作用,如桌面滚动卡滞?@AnujAroshA-Nope无副作用。:)不管怎样,我有这样的事情。可能是因为不同的原因。我查一下。
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(yourmethod) name:UIApplicationDidBecomeActiveNotification object:nil];
-(void)yourmethod{
[yourtableview reloadData];
}