我有关于重新加载 View 的问题。
我在我的应用程序中使用了标签栏。当我按下主页按钮并且我的应用程序处于后台时。现在我的问题是现在开始。每次显示我的第一个选项卡时,我都希望如此。
所以,每次我看到第一个标签栏时。但是当我得到第一个标签栏的 View 时,我在 AppDelegate 中调用第一个标签栏的 viewController 的 viewDidAppear()
,如下所示:
- (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()
被调用并加载数据并打印日志。但 View 不会重新加载或刷新。意味着在 tableview 中没有任何效果。从服务器加载的数据显示在表格 View 中。
我使用以下代码来重新加载或刷新 View :
[self.view setNeedsLayout];
[self.view setNeedsDisplay];
[tabledata reloadData];
但不受影响。
提前致谢。
最佳答案
当你打电话时:
simpleTableViewController *s = [[simpleTableViewController alloc]initWithNibName:@"simpleTableViewController" bundle:nil];
[s viewDidAppear:YES];
您只是在创建该 View Controller 的新 实例,并尝试要求它刷新数据。不是已经在导航堆栈中的 View Controller 。
我建议您将 simpleTableViewController
添加为“应用程序已激活”或“将进入前台”通知的观察者。例如:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
然后在通知处理程序中您可以重新加载您的数据:
- (void)applicationWillEnterForeground:(NSNotification *)notification {
[self reloadDataFromWeb];
[self.tableView reloadData];
}
不要忘记在 simpleTableViewController
的 dealloc
方法中移除观察者。
关于ios - 当我的应用程序进入前台时, View 不会重新加载刷新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15971659/