ios - 当我的应用程序进入前台时, View 不会重新加载刷新数据

标签 ios xcode

我有关于重新加载 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];
}

不要忘记在 simpleTableViewControllerdealloc 方法中移除观察者。

关于ios - 当我的应用程序进入前台时, View 不会重新加载刷新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15971659/

相关文章:

iphone - 让 iPad 应用程序变得通用?

ios - "clang quit unexpectedly"(xcode 崩溃)

iPhone核心数据: Date Default Value as Current date

ios - 为什么不推荐使用 MonoTouch 进​​行 iPhone 开发?

ios - 记住我使用 UISwitch 和 NSUserDefaults ios 的功能

ios - 如何从默认 UITextField 中删除 "border-radius"?

ios - iOS 模拟器可以记住使用 Retina iPhone 和非 Retina iPad 吗?

ios - 依赖项 `Mixpanel` 未在任何具体目标中使用

ios - 如何阐明其在位置模态中的使用目的?

iphone - 同一 viewController iphone 上的多个 Web 服务调用