Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dismissViewControllerAnimated calling method in another view

I am new to iOS Programming and now i have a problem.

There is a login screen that use [self presentViewController:loginview animated:YES completion:nil]; to show it out in MasterViewController.

And now i want to call a method (reload data) after [self dismissViewControllerAnimated:YES completion:nil]

this is my code:

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

but it is not working.

(the reload method is in the MasterViewController)

Anyone can help me?

like image 205
He Yifei 何一非 Avatar asked Apr 01 '26 05:04

He Yifei 何一非


2 Answers

You can use NSNotificationCenter for your problem.

Define a NSNotification in your MasterViewController viewDidLoad like below

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

And then define the method as below

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

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

}

And at last from your other controller from where you are actually trying to dismiss your controller use code below

[[NSNotificationCenter defaultCenter] postNotificationName:@"CloseModal" object:self];
like image 177
iphonic Avatar answered Apr 02 '26 19:04

iphonic


what you should do is basically call method on presenting view controller as below

[(MasterViewController*)self.presentingViewController reloadData];
like image 25
2intor Avatar answered Apr 02 '26 20:04

2intor