A snippet of the default code in a Master-Detail Xcode project
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController; // *** here ***
MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
AppDelegate.h
@property (strong, nonatomic) UIWindow *window;
I am aware that @synthesize just sets the accessor methods, and no initialization happens automagically. But how does window
have a non-nil rootViewController
if it is never explicitly initialized? Is this just Xcode init'ing behind the scenes?
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.
In all the apps built prior iOS 13, AppDelegate is the main entry of the app and it is the place where many logics and app states will be handled. It is the place where application launch and apps foreground and background logics are handled.
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.
From my book:
If you choose the Storyboard option as you specify a template, the process works a little differently. The app is given a main storyboard, pointed to by the Info.plist key “Main storyboard file base name” (
UIMainStoryboardFile
). AfterUIApplicationMain
instantiates the app delegate class, it asks the app delegate for the value of itswindow
property; if that value is nil, the window is created and assigned to the app delegate’swindow
property. The storyboard’s initial view controller is then instantiated and assigned to the window’srootViewController
property, with the result that its view is placed in the window as its root view; the window is then sent themakeKeyAndVisible
message. All of that is done behind the scenes byUIApplicationMain
, with no visible code whatever. That is why, in a storyboard template, theapplication:didFinishLaunchingWithOptions:
implementation is empty.
From the UIWindow
documentation:
Note: When you use storyboards and the Xcode app templates to create an app, a window is created for you.
If you don't use storyboards, the window is explicitly created, though all the standard project templates do this out of the box. You'll see a line similar to this in the app delegate:
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
Using storyboards, the window is created behind the scenes when the main storyboard is loaded (see the View Controller Programming Guide for more info).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With