iphone - What is the relationship between AppDelegate, RootViewController, and UIApplication? -


i trying figure out relationship between appdelegate, rootviewcontrooler, , uiapplication. here kinda have figured out far:

when starting application up, main.m gets loaded.

from here, mainwindow.xib gets loaded.

in mainwindow.xib, file's owner of type uiapplication.

you set uiapplication's delegate appdelegate.

in appdelegate's source code, can set rootviewcontroller first view shown.

is right? prompts appdelegate run it's

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { } 

method?

when objective-c application starts, starts running function named main(). doesn't have in file "main.m" that's how xcode wizard sets things up.

inside wizard-produced main() function, there line:

int retval = uiapplicationmain(argc, argv, nil, nil); 

that starts "uikit" framework makes entire application. inside uiapplicationmain, object of type uiapplication created. , part of uiapplication when application starts call applicationdidfinishlaunchingwithoptions method on delegate member of uiapplication class. delegate set in mainwindow.xib file instance of projectappdelegate class, subclass of nsobject conforms uiapplicationdelegate protocol.

what prompts appdelegate run it's ...

because in mainwindow.xib file have connected (well project wizard did connection actually) file's owner (which uiapplication object)'s "delegate" outlet uiapplicationdelegate object in the .xib file, , class of uiapplicationdelegate set app's uiapplicationdelegate subclass.

and there's nothing magic "mainwindow.xib", called "foo.xib", what's important property in info.plist file called "main nib file base name" "mainwindow". trying renaming mainwindow.xib foo.xib , changing "main nib file base name" in info.plist "foo" , you'll see still works.

edit: more rootcontroller

again, there's nothing magic so-called "rootcontroller". name of uiviewcontroller subclass created xcode new project wizard.

the wizard places code in project 2 classes: projectappdelegate , projectviewcontroller. projectappdelegate class contains 2 outlet members:

iboutlet uiwindow *window; iboutlet projectviewcontroller *viewcontroller; 

in mainwindow.xib file, instances of both uiwindow , projectviewcontroller placed, , hooked above outlets in projectappdelegate.

what gets stuff on screen code in projectappdelegate class:

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {          // override point customization after application launch.      // add view controller's view window , display.     [self.window addsubview:viewcontroller.view];     [self.window makekeyandvisible];      return yes; } 

again, nothing magic this: project wizard created code adds "root" viewcontroller's view window's view, , makes window visible. "root" view controller create in .xib file, , hooked projectappdelegate outlet.

it instructive try create application entirely without using of files wizard. you'll learn lot how .xib files work , how relate code objects.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -