When should I use init:
and when should I use initWithNibName:bundle:
when creating a view controller?
相关问题
- CALayer - backgroundColor flipped?
- thread_local variables initialization
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
相关文章
- 现在使用swift开发ios应用好还是swift?
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- How do you detect key up / key down events from a
- “Storyboard.storyboard” could not be opened
You can just call init, as long as the xib has the same name as the view controller class. The encapsulation is not necessary. This saves typing, but may not serve clarity.
Use initWithNibName if you are... initializing with a nib file! That is, a file that you made using Interface Builder.
If you aren't using IB to layout your views, you can just use init.
-initWithNibName:bundle:
is the designated initializer for UIViewController. Something should eventually call it. That said, and despite Apple's examples (which favor brevity over maintainability in many cases), it should never be called from outside the view controller itself.You will often see code like this:
I say this is incorrect. It puts implementation details (the name of the NIB and the fact that a NIB is even used) into the caller. That breaks encapsulation. The correct way to do this is:
Then, in MYViewController:
This moves the key implementation details back into the object, and prevents callers from accidentally breaking encapsulation. Now if you change the name of the NIB, or move to programmatic construction, you fix it in one place (in the view controller) rather than in every place the view controller is used.
using init when there is no nib/xib file, e.g. UI are created by coding
using initWithNibName , if we have an nib/xib or same controller share by more than 1 nib/xib
that's what I think..