When should I use the addSubview method of a view controller?

蹲街弑〆低调 提交于 2020-01-15 02:55:28

问题


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.


回答1:


-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.




回答2:


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.



来源:https://stackoverflow.com/questions/2178883/when-should-i-use-the-addsubview-method-of-a-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!