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.
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];
}];
You want something like:
[self dismissViewControllerAnimated:YES completion:^{
...
<do something on completion here>
...
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With