can someone explain what is app delegate?
can someone explain what is app delegate?
Look at the following, it's a very straight forward AppDelegate
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
This is a UIResponder object, that is a NSObject. A generic "AppDelegate" interface must conform the UIApplicationDelegate delegate.
This is where your app life cycle stars, for what regards the User Interface:
And just learn those delegates methods (these ones are not all, but the commons). I'm leaving the most important at the end of the list.
/*Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.*/
- (void)applicationWillResignActive:(UIApplication *)application {
}
/*Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.*/
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
/*Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.*/
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
/*Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.*/
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
/*Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.*/
- (void)applicationWillTerminate:(UIApplication *)application {
}
And the very entry point:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = myUIViewMainController;
[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window makeKeyAndVisible];
return YES;
}
You can see that we are allocating a UIWindow (self.window) with the screen size frame. Then we set the Root UIViewController object instance on this UIWindow instance (self.window.rootViewController). We may set a background color for the window, and then we make it the KeyWindow and visibile:
- (void)makeKeyWindow;
- (void)makeKeyAndVisible; // convenience. most apps call this to show the main window and also make it key. otherwise use view hidden property
In Objective C, delegates are objects that handle some functionality for another object. This is accomplished through a protocol, which is a set of defined methods. If an object adopts that protocol, it can be a delegate for that other object.
There are multiple benefits to this. With delegates, you can eliminate subclassing, which is what you would have to use if you wanted to allow one object to call methods in another. It also helps because the class that is calling those methods knows nothing about the other object. It only knows that it conforms to the protocol, and those methods can be called.
So an app delegate is an object that the application object can use to do certain things like display the first window or view when the app starts up, handle outside notifications or save data when the app goes into the background.
If you're interested in what the docs say about that, see: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html?hl=ar
To make a long story short, delegates are basically a callback mechanism where created objects can call methods that are in the object that created them.
This is a perfect overview of what a delegate does and how to use it.