Why does Apple recommends to create modal navigati

2019-03-27 04:01发布

问题:

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