ios - UIViewController - alloc and init vs. instantiate? -
what difference between following 2 ways of instantiating view controllers?
myviewcontroller *vc = [[myviewcontroller alloc] init]; // present vc...
vs.
[[uistoryboard storyboardwithname:@"main" bundle:nil] instantiateviewcontrollerwithidentifier:@"myviewcontrollerscene"]
it seems alloc init
can magically identify proper scene in storyboard; how happen? invoking instantiateviewcontrollerwithidentifier:
under hood? preferred way of instantiating view controller? first way result in memory leak or extraneous view controller instances?
[[myobject alloc] init]
creates new object. it's not retrieving object storyboard, allocating memory , instantiating it.
instantiateviewcontrollerwithidentifier:
creates new view controller (if identifier exists in storyboard) , configures according how view controller configured object in storyboard file.
both cases create new instance each call.
if have configured view controller in storyboard (for example connected outlets, actions, etc.) , want retrieve it, should read storyboard. if create new instance (not storyboard) not have configuration.
Comments
Post a Comment