我试图了解如何更新viewController
当前可见的作品。我有ViewControllerA
和ClassA
。我想告诉ViewControllerA
reloadDatatableview
来自ClassA
。这样做的最佳方法是什么?
我已经找到了这个问题和答案,但是我认为这不会在我的情况下起作用,或者我无法正确理解。
不知道您的设置的最简单方法就是使用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];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句