xcode 当我的应用程序进入前台时,视图不会重新加载刷新数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15971659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When my app enters foreground, view is not reloaded with refresh data
提问by kels
I have a problem regarding reload of view.
我有关于重新加载视图的问题。
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:
我在我的应用程序中使用 tabbar。当我按下主页按钮并且我的应用程序在后台时。现在我的问题是现在开始。每次显示我的第一个标签时,我都希望这样做。所以,每次我获得第一个标签栏的视图时。但是,当我获得第一个 tabbar 的视图时,我viewDidAppear()
会在 AppDelegate 中调用第一个 tabbar 的 viewController,如下所示:
- (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];
}
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.
因此,viewDidAppear()
被调用并加载数据并打印日志。但是视图不会重新加载或刷新。意味着在 tableview 中没有任何影响。从服务器加载的数据显示在 tableview 中。
I use the following code for reload or refresh view:
我使用以下代码重新加载或刷新视图:
[self.view setNeedsLayout];
[self.view setNeedsDisplay];
[tabledata reloadData];
But it is not effected.
但它没有生效。
Thanks in advance.
提前致谢。
回答by Ian L
When you're calling:
当你打电话时:
simpleTableViewController *s = [[simpleTableViewController alloc]initWithNibName:@"simpleTableViewController" bundle:nil];
[s viewDidAppear:YES];
You are simply creating a newinstance of that view controller and attempting asking it to refresh it's data. Not the view controller that is already in the navigation stack.
您只是创建该视图控制器的新实例并尝试要求它刷新其数据。不是已经在导航堆栈中的视图控制器。
I suggest that you add your simpleTableViewController
as an observer for the "application did become active" or "will enter foreground" notification. E.g.:
我建议您将自己添加simpleTableViewController
为“应用程序已激活”或“将进入前台”通知的观察者。例如:
- (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];
}
Don't forget to remove the observer in the simpleTableViewController
's dealloc
method.
不要忘记删除simpleTableViewController
'sdealloc
方法中的观察者。
回答by Phillip Mills
Do not call viewDidAppear
. Show your view controller and iOS will call your viewDidAppear
for you when the view truly appears.
不要打电话viewDidAppear
。显示您的视图控制器,viewDidAppear
当视图真正出现时,iOS 会为您调用。