Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dismissModalViewControllerAnimated works on self but not parentViewController

Tags:

ios5

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:

  1. I have a UITableViewController with a nav button that calls up a modal view:


    - (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!

like image 492
LeoAlmighty Avatar asked Dec 02 '22 00:12

LeoAlmighty


2 Answers

[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];
}
like image 140
NJones Avatar answered Jun 08 '23 00:06

NJones


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.

like image 30
Jas Avatar answered Jun 08 '23 00:06

Jas