Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am trying to figure out the relationship between the appdelegate, RootViewControoler, and UIApplication. Here is what I kinda have figured out so far:

When starting your application up, main.m gets loaded.

From here, your MainWindow.xib gets loaded.

In your MainWindow.xib, your File's Owner is of type UIApplication.

You set your UIApplication's delegate to your AppDelegate.

In your AppDelegate's source code, you can set your RootViewController to be the first view shown.

Is this right? What prompts AppDelegate to initially run it's

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { } 

method?

like image 679
guy8214 Avatar asked Feb 10 '11 06:02

guy8214


People also ask

What is the difference between AppDelegate and SceneDelegate?

AppDelegate is responsible for handling application-level events, like app launch and the SceneDelegate is responsible for scene lifecycle events like scene creation, destruction and state restoration of a UISceneSession.

What is UIApplication delegate?

The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system. Like the UIApplication object, UIKit creates your app delegate object early in your app's launch cycle so it's always present.

Is AppDelegate class Singleton?

AppDelegate is however a singleton class but you show only use it for declaring things that applies globally in your application. For ex: If you want to change the color of navigation bar in your entire application you can use app delegate and set the color of navigation bar.

Is AppDelegate a controller?

The application delegate is a controller object. By default, it is the owner and controller of the main window -- which is a view -- in an iOS app.


2 Answers

When an Objective-C application starts, it starts by running the function named main(). It doesn't have to be in the file "main.m" but that's how the Xcode wizard sets things up.

Inside the wizard-produced main() function, there is this line:

int retVal = UIApplicationMain(argc, argv, nil, nil); 

That is what starts the "UIKit" framework that makes up the entire application. Inside UIApplicationMain, an object of type UIApplication is created. And part of what UIApplication does when the application starts is call the applicationDidFinishLaunchingWithOptions method on the delegate member of the UIApplication class. This delegate is set up in the MainWindow.xib file to be an instance of your ProjectAppDelegate class, a subclass of NSObject that conforms to the UIApplicationDelegate protocol.

What prompts AppDelegate to initially run it's ...

Because in your MainWindow.xib file you have connected (well the project wizard did the connection actually) the File's Owner (which is the UIApplication object)'s "delegate" outlet to the UIApplicationDelegate object in the the .xib file, and the class of the UIApplicationDelegate is set to your app's UIApplicationDelegate subclass.

And there's nothing magic about "MainWindow.xib", it could be called "Foo.xib", what's important is that the property in your Info.plist file called "Main nib file base name" is "MainWindow". Trying renaming MainWindow.xib to Foo.xib and changing the "Main nib file base name" in your Info.plist to "Foo" and you'll see it still works.

EDIT: more about RootController

Again, there's nothing magic about the so-called "RootController". This is just the name of the UIViewController subclass created for you by the Xcode new project wizard.

The wizard places code in the project for two classes: ProjectAppDelegate and ProjectViewController. The ProjectAppDelegate class contains two outlet members:

IBOutlet UIWindow *window; IBOutlet ProjectViewController *viewController; 

in the MainWindow.xib file, instances of both UIWindow and ProjectViewController are placed, and hooked up to the above outlets in ProjectAppDelegate.

What gets your stuff up on the screen is this code in your ProjectAppDelegate class:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {          // Override point for customization after application launch.      // Add the view controller's view to the window and display.     [self.window addSubview:viewController.view];     [self.window makeKeyAndVisible];      return YES; } 

Again, nothing really magic about this: the project wizard created code that adds your "root" ViewController's view to the window's view, and makes the window visible. Your "root" view controller was create in the .xib file, and hooked up to the ProjectAppDelegate outlet.

It is very instructive to try to create an application entirely by yourself without using any of the files from the wizard. You'll learn a lot about how .xib files work and how they relate to code objects.

like image 134
Bogatyr Avatar answered May 27 '23 01:05

Bogatyr


The starting point of iOS apps is always the main() function (thanks @bogatyr) which usually contains code similar to,

int main(int argc, char *argv[]) {     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];     int retVal = UIApplicationMain(argc, argv, nil, nil);     [pool release];     return retVal; } 

The last two parameters of UIApplicationMain are important and specify the principal class name, and the application delegate. If they are nil, then Info.plist will be looked up for the main window xib (usually MainWindow.xib).

// If nil is specified for principalClassName, the value for NSPrincipalClass // from the Info.plist is used. If there is no NSPrincipalClass key specified, the // UIApplication class is used. The delegate class will be instantiated  // using init. .. UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName); 

It is not necessary to set the File Owner through xib, and they can be specified directly in this UIApplicationMain function.

principalClassName can be the string UIApplication or a subclass of UIApplication. Similarly delegateClassName can be directly specified in this method. The delegate class is instantiated using init as the docs say. Suppose we specify our delegate class - MyAppDelegate as a string,

UIApplicationMain(int argc, char *argv[], nil, @"MyAppDelegate"); 

First an instance of UIApplication is instantiated, which will then create the delegate class from this string using NSClassFromString I suppose.

Once delegateObject has been instantiated, and application is ready, this delegateObject will be informed using the delegate method, didFinishLaunchingWithOptions.

Class delegateClass = NSClassFromString(@"MyAppDelegate"); id <UIApplicationDelegate> delegateObject = [[delegateClass alloc] init];  // load whatever else is needed, then launch the app // once everything is done, call the delegate object to // notify app is launched [delegateObject application:self didFinishLaunchingWithOptions:...]; 

This is how UIApplication would handle it programmatically, if no nib is used. Using a nib in the middle is not much different.

like image 22
Anurag Avatar answered May 27 '23 03:05

Anurag