使用Objective-C从其他ViewController重新加载TableView

Man*_*sha 2 objective-c reload uitableview ios

我不确定如何实现这一目标,因此我在问这个问题。

我有2个名为abVC和的ViewController(VC)xyVCabVC只包含TableViewxyVC只包含一个按钮。

当我点击一个按钮时xyVC,它应该重新加载tableview abVC而不移动到该视图abVC

可能吗 ?如果是..那么请帮助我,该怎么做?

iOS*_*MIB 5

您可以同时使用block和NSNotification来实现。

使用块,您可以按以下方式实现它:

在xyzVC.h中创建如下属性:

@property(strong,nonatomic)void(^callBack)();
Run Code Online (Sandbox Code Playgroud)

在xyzVC.m中合成此属性

@synthesize callBack
Run Code Online (Sandbox Code Playgroud)

在Buttons click事件中调用此块

- (IBAction)btnClick:(UIButton *)sender {

if (callBack)
    {
        callBack();
    }

}
Run Code Online (Sandbox Code Playgroud)

在您想回叫的abcVC中实现它:

[abVC setCallBack:^{
        //reload your tableview here
    }];
Run Code Online (Sandbox Code Playgroud)

使用NSNotification,您可以按以下方式实现它:

在abcVC.m中注册以接收通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableView) name:@"ReloadTable" object:nil];

- (void)reloadTableView{
  // Reload your tableview here
}
Run Code Online (Sandbox Code Playgroud)

现在从xyzVC发布通知

- (IBAction)btnClick:(UIButton *)sender {

  [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadTable" object:nil];

}
Run Code Online (Sandbox Code Playgroud)

注意:如果您使用的是NSNotification,请不要忘记在关闭视图控制器后将其删除

我希望这能帮到您 :)