另一个视图中的dismissViewControllerAnimated调用方法

He *_*何一非 3 objective-c ios

我是iOS编程新手,现在我遇到了问题.

有一个登录屏幕用于[self presentViewController:loginview animated:YES completion:nil];在MasterViewController中显示它.

现在我想在之后调用一个方法(重新加载数据) [self dismissViewControllerAnimated:YES completion:nil]

这是我的代码:

[self dismissViewControllerAnimated:YES completion:^ {
   [self getPostData];                 // Reload Data
   [self.tableView reloadData];        // Reload Data
}];
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

(重载方法在MasterViewController中)

有人可以帮帮我吗?

iph*_*nic 8

您可以使用NSNotificationCenter您的问题.

MasterViewController viewDidLoad中定义NSNotification,如下所示

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeModal:) name:@"CloseModal" object:nil];
Run Code Online (Sandbox Code Playgroud)

然后定义如下方法

-(void)closeModal:(NSNotification *)notification{
    UIViewController *controller=(UIViewController *)notification.object;

    [controller dismissViewControllerAnimated:YES completion:^ {
         [self getPostData];                 // Reload Data
         [self.tableView reloadData];        // Reload Data
    }];

}
Run Code Online (Sandbox Code Playgroud)

最后,从你的其他控制器,你实际上试图解雇你的控制器使用下面的代码

[[NSNotificationCenter defaultCenter] postNotificationName:@"CloseModal" object:self];
Run Code Online (Sandbox Code Playgroud)