He Yifei 何一非
He Yifei 何一非

Reputation: 2642

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?

Upvotes: 3

Views: 2651

Answers (3)

Nupur Daddikar
Nupur Daddikar

Reputation: 89

You can call the presentingViewController's methods which updates the Presenting view.

for example: I have a modal view which can be launched from multiple Views. And all the presenting views have the view reloading code within view will appear.

Add the below code before dissmissing the modal view.

        [self.presentingViewController viewWillAppear:YES];

[self dismissViewControllerAnimated:YES completion:Nil];

Upvotes: 0

2intor
2intor

Reputation: 1044

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

[(MasterViewController*)self.presentingViewController reloadData];

Upvotes: 1

iphonic
iphonic

Reputation: 12719

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];

Upvotes: 9

Related Questions