I got dismissModalViewControllerAnimated to work properly on the following setup but am confused as to why it works on self (the modalViewController) rather than the parentViewController.
Here's the setup:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Root";
_data = [NSArray arrayWithObjects:@"One", @"Two", nil];
_detailController = [[DetailViewController alloc] init];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showAbout)];
}
- (void)showAbout
{
AboutViewController *abv = [[AboutViewController alloc] init];
abv.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:abv animated:YES];
}
Here's the modal view controller AboutViewController with a toolbar button that triggers a dismiss action to close the modal:
- (IBAction)dismissAction:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
My question is why does [self dismissModalViewControllerAnimated] work rather than [self.parentViewController dismissModalViewControllerAnimated] ?? Is this new in iOS 5? I thought only the parentViewController is capable of dismissing a child modal view?
Thanks!
[self dismissModalViewControllerAnimated:YES];
Has worked for quite some time. One of the best kept secrets in iOS development if you ask me.
However self.parentViewController
not working is in fact new to iOS 5. It has been 'replaced' with self.presentingViewController
.
This causes an interesting problem for code trying to be pre-iOS 5 compatible. Since as you have found self.parentViewController
returns nil on iOS 5. And UIViewControllers
do not respond to presentingViewController
pre-iOS 5.
It leaves us doing something like this:
if ([self respondsToSelector:@selector(presentingViewController)]){
[self.presentingViewController dismissModalViewControllerAnimated:YES];
}
else {
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
Instead of using what NJones has said, I would suggest sticking to
[self dismissModalViewControllerAnimated:YES]
The reason this will work for all the OSes is stated in the documentation itself:
"The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, however, it automatically forwards the message to the presenting view controller."
NOTE: A note about this method in iOS5.0 though. dismissModalViewControllerAnimated
: method has been deprecated. dismissViewControllerAnimated:completion
: should be used over here instead.
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