ios – 从另一个UIViewController刷新表

我有两个视图控制器,其中一个(ViewController)有一个名为tableView的表.

我想从我的其他视图控制器(pageView)刷新此表.

我在pageView中试过这个:

ViewController*refresh;
[refresh.tableView reloadData];

但这不起作用.

两个视图控制器之间的连接segue是推送segue

我该怎么办?我应该通过故事板segue做到吗?

解决方法

选项1

@类2

@property (nonatomic) BOOL shouldRefresh; // in .h file

- (void)viewWillAppear:(BOOL)animated // in .m file
{
    [super viewWillAppear:animated];
    if (_shouldRefresh) [self.tableView reloadData];
}

@ Class1的

// Add this in any method where you want it to refresh and push to view
ClassTwoController *viewController = [[ClassTwoController alloc] init];
[viewController setShouldRefresh:YES];
[self.navigationController pushViewController:viewController animated:YES];

*更新:

选项2

@Class 2

// Add this line in viewDidLoad in same class you want the tableView to refresh
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTableWithNotification:) name:@"RefreshTable" object:nil];

// Add this method just beneath viewDidLoad:
- (void)refreshTableWithNotification:(NSNotification *)notification
{
    [self.tableView reloadData];
}

@ Class1的

// Call this when ever you want to refresh the tableView in Class2
[[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshTable" object:nil userInfo:nil];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...