views:

395

answers:

2

I'm programming for the iPhone and I'm wondering when to use the addSubview method of a view and when to present to use the modal view controller (presentModalViewController). What complicates this even more is if you are using a navigation controller (I'm not) and you can use the pushViewController method?

When would you use each and why?

Thanks.

+2  A: 

-presentModalViewController and -pushViewController are two ways of going about the same thing: displaying a new view. Which you use depends on the user experience you're going for. They mean different things to the user, but are very similar in implementation.

-addSubview is completely different. It adds components to the current view. You should never use it to display an independent UI. -addSubview is most often used when programmatically creating the UI in -loadView, though it has many other uses.

Rob Napier
When would you use presentModalViewController and pushViewController? I'm not sure when I should use which.
Anthony Glyadchenko
Modal: http://developer.apple.com/iPhone/library/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html NavController: http://developer.apple.com/iPhone/library/featuredarticles/ViewControllerPGforiPhoneOS/NavigationControllers/NavigationControllers.html
Rob Napier
+1  A: 

Here's one way to look at it:

A sequence of view-controllers within single navigatoin controller represent a single workflow in user's head. If at some point you need to interrupt the current workflow and create a diverging workflow you create a modal dialog. If the new workflow only has one step you simply present corresponding controller, but if there are many steps you create a new navigation controller to string the steps together.

The visuals are different - with nav controller user's attention moves from left to right, while with modal dialog from top to bottom. Imagine that you are flipping a book (left-to-right) and at some point you move the book away from you and then pull another book from under the table and place it in front of you (top to bottom), and then start going through that another book (left-to-right). Then you close the whole second book and move back to first book where you left off.

The addSubview method is on a different abstraction plane - subviews are used to create the two experiences I described above. You can use subviews to create a different experience which would be on the same abstraction level. Couple more examples of the constructs on the same level are UIAlertView and UIActionSheet.

DenNukem