ios - For completely programmatically created UIView/UIViewController, what should be in loadView vs viewDidLoad? -
for experimentation purposes, trying in code.
i have custom root uiview
/uiviewcontroller
subclass pair called rootview , rootviewcontroller respectively.
i have glkview
/glkviewcontroller
subclass pair called renderview , renderviewcontroller.
both uiviewcontroller
subclasses create managed views in loadview
overrides.
setting main rootviewcontroller/rootview simple. in appdelegate subclass, create main window , assign rootviewcontroller proeprty so...
#import "appdelegate.h" #import "rootviewcontroller.h" @implementation appdelegate - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { cgrect screenbounds = [[uiscreen mainscreen] bounds]; // set window uiwindow* mainwindow = [[uiwindow alloc] initwithframe:screenbounds]; mainwindow.rootviewcontroller = [rootviewcontroller new]; self.window = mainwindow; [mainwindow makekeyandvisible]; return yes; } @end
the rootview appears on screen 1 expect.
what i'm wondering view hierarchy initialization. rootviewcontroller has property hold instance of renderviewcontroller, , renderview (initially) placed in frame stored in second property of rootviewcontroller called renderviewframe.
my question proper place set hierarchy?
the documentation states that's uiviewcontroller
's loadview
i'm doing there...
- (void)loadview { self.view = [[rootview alloc] initwithframe:[[uiscreen mainscreen] bounds]]; // set renderviewcontroller renderviewcontroller* renderviewcontroller = [renderviewcontroller new]; [self addchildviewcontroller:renderviewcontroller]; renderviewcontroller.view.frame = self.renderviewframe; [self.view addsubview:renderviewcontroller.view]; self.renderviewcontroller = renderviewcontroller; }
...and works, i've seen people no, should move relating creating renderview/renderviewcontroller rootviewcontroller's viewdidload
, not loadview
, no 1 can why. mean part of hierarchy , that's loadview
for.
and if i'm right, why i/anyone need use viewdidload
@ if we're doing initialization in loadview
, or artifact when memory unload views way in ios6?
your solution fine programmatic solution. since need implement loadview
can initialization here.
you can mixed loadview
, viewdidload
solution (only set view
property in loadview
, rest in viewdidload
); might make easier go nib/story board solution later on. having initialization in 1 place (just loadview
) might easier maintain having split in 2 (loadview
, viewdidload
). on other hand, people expect see viewdidload
method. can see, it's matter of taste.
when using xibs or story boards, of course it's recommended avoid touching loadview
@ , should implement viewdidload
.