Reputation: 1557
This should be simple for some of you.
I am dismissing a UIViewController
that is presented modally by another UIViewController
from within itself. I am doing this using
[self dismissViewControllerAnimated:YES
completion:^{
// Want to access presentingViewController here.
}];
I tried logging self
's navigationController
, parentViewController
and presentingViewController
, but they all return null
.
Apple's doc says :
The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.
According to this, the message is sent to the presentingViewController
. I want to know how to catch this message. Is there a delegate call or how can I go about finding if the presentedViewController was dismissed.
Upvotes: 0
Views: 950
Reputation: 57040
There is no public API that would tell you this. You could listen to viewWillAppear:
and figure out this way that the presented view controller is being dismissed, but that is messy. I will recommend what I always recommend and is considered best practice: never dismiss a view controller from itself! Create a delegate protocol for your presented view controller, set the delegate to the presenting view controller, and when there is a need to dismiss the presented from the presenting, call the delegate.
Why not put aside the presenting view controller, and access it inside the block after dismissal?
__weak id weakPresentingViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion: ^
{
id strongPresentingViewController = weakPresentingViewController;
if(strongPresentingViewController == nil) return;
//Do what you will with the presenting view controller.
}
This is the least error prone method, taking into account your current implementation. If the presenting view controller gets released before your block is called, you will not hit a bad access crash. Inside the block, you capture it strongly to make sure it is not released in the middle of your code.
Upvotes: 1