Why does Apple recommends to create modal navigation controllers programmatically?

♀尐吖头ヾ 提交于 2019-12-12 07:50:37

问题


"When presenting a navigation controller modally, it is often simpler to create and configure your navigation controller object programmatically. Although you could also use Interface Builder to do so, doing so is generally not recommended." If fact, because my navigation controller is simple, I would rather customise the view in IB!


回答1:


While I don't know the exact reason, this is my shot at it:

When creating the controller programmatically, you need only a few lines of code, in fact, in most cases this might be really few: creating, setting the root controller, presenting and releasing. The alternatives are quickly enumerable:

  • You could hold the view controller as an outlet in the underlying controller. Then, however, it would reside in memory all the time. Would not only be waste of memory, but this also doesn't make too much sense while it's not needed.
  • You could instantiate that controller form a nib file. Then, however, you would have to do a lot of things that you'd do either way:
    • Create the controller in code with alloc,init
    • Set up some properties - either in a custom class, the nib or a few lines of code
    • Present it
    • Release it

Now, given that the alternative is only a few lines setup of code, the overhead of loading a nib file, which is in fact not extremely cheap, isn't really worth the additional comfort. If you are doing a lot of setup then this would go into a custom class anyways, no matter whether loading from nib or creating in code.

Just my thoughts...



来源:https://stackoverflow.com/questions/3746183/why-does-apple-recommends-to-create-modal-navigation-controllers-programmaticall

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