Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dismissViewControllerAnimated completion method not working

Here's my app design. I have mainController which presents secondViewController. Now, I want to dismiss secondViewController and subsequently call the method aMethod on mainController like so:

[self dismissViewControllerAnimated:YES completion:aMethod];

But this gives me the error use of undeclared identifier 'aMethod'

Obviously I am not using the completion handler correctly, but I cannot figure out the correct way.

like image 603
Jonah Katz Avatar asked Oct 26 '12 00:10

Jonah Katz


2 Answers

I think this what you are looking for,

[self dismissViewControllerAnimated:YES completion:^{
            [self.mainController aMethod];
        }];

In the above code you need to declare self outside the block and use it as,

__block SecondViewController *object = self;

[self dismissViewControllerAnimated:YES completion:^{
                [object.mainController aMethod];
            }];

Just to avoid self getting retained in block.

Update:

Got the issue now. You need to declare mainController as a property in your .h file of secondViewController. After that when you are presenting the secondViewController from maincontroller, you need to set it as,

secondViewController.maincontroller = self;
[self presentViewController:secondViewController animated:YES completion:Nil];

In your SecondViewController.h file,

@property(nonatomic, assign) MainController *mainController;

In your SecondViewController.m file,

@synthesis mainController;

Update 2:

If you do not want to declare maincontroller as a property, try this. I am not sure whether this is the right way to do. But it looks like it used to work.

 MainController *mainController = (MainController *)[self.view.superview nextResponder];

    [self dismissViewControllerAnimated:YES completion:^{
                    [mainController aMethod];
                }];

Update 3(Suggested):

This should work for you. Check it.

 MainController *mainController = (MainController *)self.parentViewController;

    [self dismissViewControllerAnimated:YES completion:^{
                    [mainController aMethod];
                }];
like image 104
iDev Avatar answered Nov 01 '22 19:11

iDev


You want something like:

[self dismissViewControllerAnimated:YES completion:^{
            ...
            <do something on completion here>
            ...
        }];
like image 20
EarlyRiser Avatar answered Nov 01 '22 17:11

EarlyRiser