我试图了解如何更新当前可见的viewController
我有ViewControllerAClassA。我想告诉ViewControllerAtableview重新加载ClassA上的数据。这样做的最佳方法是什么?

我找到了this问题和答案,但是我认为这在我的情况下不起作用,或者我无法正确理解。

最佳答案

不知道您的设置的最简单方法是使用NSNotificationCenter。您可以执行以下操作:

ViewControllerA中添加NSNotificationCenter的钩子:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    //Register for notification setting the observer to your table and the UITableViewMethod reloadData. So when this NSNotification is received, it tells your UITableView to reloadData
    [[NSNotificationCenter defaultCenter] addObserver:self.table selector:@selector(reloadData) name:@"ViewControllerAReloadData" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    //Need to remove the listener so it doesn't get notifications when the view isn't visible or unloaded.
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

然后在ClassA中,当您想告诉ViewControllerA重新加载数据时,只需发布​​NSNotification即可。
- (void)someMethod {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ViewControllerAReloadData" object:nil];
}

关于ios - 如何告诉ViewController从另一个类更新其UI,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21003291/

10-10 20:38