当我的应用程序进入前台时,视图不会重新加载刷新数据 [英] When my app enters foreground, view is not reloaded with refresh data
问题描述
我有关于重新加载视图的问题。
I have a problem regarding reload of view.
我在我的应用程序中使用tabbar。当我按下主页按钮,我的应用程序在后台。现在我的问题是从现在开始。每当我的第一个标签显示时,我希望这样。
因此,每次我获得第一个标签栏的视图。但是当我获得第一个标签栏的视图时,我在AppDelegate中调用第一个tabar的viewController的 viewDidAppear()
,如下所示:
I use tabbar in my application. When I press home button and my app is in background. Now my problem is start now. I Want that every time my first tab is display.
So, every time I get 1st tabbar's view. But When I get 1st tabbar's view, I call viewDidAppear()
of 1st tabar's viewController in AppDelegate like below:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
self.tabBarController.selectedViewController=[self.tabBarController.viewControllers objectAtIndex:0];
[navigationController1 popToRootViewControllerAnimated:YES];
[navigationController2 popToRootViewControllerAnimated:YES];
[navigationController3 popToRootViewControllerAnimated:YES];
[navigationController4 popToRootViewControllerAnimated:YES];
[navigationController5 popToRootViewControllerAnimated:YES];
simpleTableViewController *s = [[simpleTableViewController alloc]initWithNibName:@"simpleTableViewController" bundle:nil];
[s viewDidAppear:YES];
}
所以, viewDidAppear()$ c $调用c>并加载数据并打印日志。但视图不会重新加载或刷新。表示tableview没有任何影响。从服务器加载的数据显示在tableview中。
So,viewDidAppear()
is called and data is loaded and log is printed. But view is not reloaded or refreshed. Means there is not any effect in tableview. Data which is loaded from server is displayed in tableview.
我使用以下代码进行重新加载或刷新视图:
I use the following code for reload or refresh view:
[self.view setNeedsLayout];
[self.view setNeedsDisplay];
[tabledata reloadData];
但不受影响。
提前致谢。
推荐答案
当你打电话时:
simpleTableViewController *s = [[simpleTableViewController alloc]initWithNibName:@"simpleTableViewController" bundle:nil];
[s viewDidAppear:YES];
您只需创建该视图控制器的 new 实例并尝试询问它刷新它的数据。不是已经在导航堆栈中的视图控制器。
You are simply creating a new instance of that view controller and attempting asking it to refresh it's data. Not the view controller that is already in the navigation stack.
我建议您将 simpleTableViewController
添加为观察者对于应用程序确实变为活动或将进入前台通知。例如:
I suggest that you add your simpleTableViewController
as an observer for the "application did become active" or "will enter foreground" notification. E.g.:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
然后在通知处理程序中,您可以重新加载数据:
Then in the notification handler you can reload your data:
- (void)applicationWillEnterForeground:(NSNotification *)notification {
[self reloadDataFromWeb];
[self.tableView reloadData];
}
不要忘记删除中的观察者simpleTableViewController
的 dealloc
方法。
这篇关于当我的应用程序进入前台时,视图不会重新加载刷新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!