ModalView on iPhone: Can't remove


I’m really stuck and need some help!

I’m trying to do what we did on the iPad on the iPhone. I have managed to get the ModalView showing on the iPhone by doing:

    // Create a new ImageViewController and set its image
    ImageViewController *ivc = [[ImageViewController alloc] initWithNibName:@"ImageViewController" bundle:nil];
    [ivc setImage:img];
    [self presentModalViewController:ivc animated:YES];

// Note that this is basically following the already provided code.

I have read the Apple documentation and searched the Internet. From the Apple documentation we have:

“The parent view controller is responsible for dismissing the modal view controller it presented using the presentModalViewController:animated: method. If you call this method on the modal view controller itself, however, the modal view controller automatically forwards the message to its parent view controller. … etc …”

So the recommendations are from Apple and many others to dismiss the ModalViewController (being aware of child/parent/child relationship between the two view controller objects) using:

 [self dismissModalViewControllerAnimated:YES];
[[self parentViewController] dismissModalViewControllerAnimated:YES];  // Still not sure on this one.

Unlike the iPad which seems to have a method to remove the popoverController, I can’t find anything to help write the header for the method to remove the ModalViewController for the iPhone.

I’ve tried ImageViewController and UIViewController in a method but I can’t get anything to work.

I was also thinking that, do you put a button on the ImageViewController to dismiss the ModalView but when I did this I couldn’t get the controls over to the ItemsViewController.

Can someone point me in the right direction so I can finish this. Its starting to bother me that I can’t solve it myself. I want to do this as I think it should be part of the book exercises.

Thanks in advance


In ImageViewController you should simply be able to have a “DONE” button somewhere, which when tapped invokes:



Thanks for that. I’ll give it a go. Now that you point that out to me I had things round the wrong way.

That app of yours looks great.



You need to instantiate a UINavigationController, set imageViewController as its root controller, and then present the navigation controller from itemsViewController.

You also need to get the imageViewController’s navigationItem, and set a UIBarButtonItem so you can have a way to dismiss the modal view of the image.

The bar button item will have a selector, which is a method back in itemsViewController. There you can dismiss the presented view.

Hope this helps.


Spot on. Thanks. Finally got the app finished.



Cool. :slight_smile: