当我的应用程序进入前台时,视图不会重新加载刷新数据
我有关于重新加载视图的问题。
我在我的应用程序中使用tabbar。 当我按下主页按钮,我的应用程序在后台。 现在我的问题是从现在开始。 每当我的第一个标签显示时,我希望这样。 所以,每次我获得第一个标签栏的视图。 但是当我获得第一个tabbar的视图时,我在AppDelegate中调用1st tabar的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()
并加载数据并打印日志。 但视图不会重新加载或刷新。 表示tableview没有任何影响。 从服务器加载的数据显示在tableview中。
我使用以下代码进行重新加载或刷新视图:
[self.view setNeedsLayout]; [self.view setNeedsDisplay]; [tabledata reloadData];
但它没有受到影响。
提前致谢。
当你打电话时:
simpleTableViewController *s = [[simpleTableViewController alloc]initWithNibName:@"simpleTableViewController" bundle:nil]; [s viewDidAppear:YES];
您只是创建该视图控制器的新实例并尝试让它刷新它的数据。 不是已经在导航堆栈中的视图控制器。
我建议您添加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
方法中删除观察者。
不要调用viewDidAppear
。 显示您的视图控制器,当视图真正出现时,iOS将为您调用viewDidAppear
。