What is the difference between view 'did load' method and 'didFinishLaunching' application - iphone

In the iPhone SDK, can anyone explain the difference between application DidFinishLaunching in delegate and ViewDidLoad method in ViewControler?

applicationDidFinishLaunching is called by the App Delgate when your application has finished launching. This method is useful for doing setup as soon as possible. Examples of this could include setting up GameCenter, and doing some first launch checking.
viewDidLoad is called by a UIViewController after the view is loaded, usually from the nib. However, in some cases, you may want to do setup before the view is loaded. In that case, use
viewWillLoad is called just before the view is loaded, usually from the nib. For the most part, it will not make much of a difference wether you use viewDidLoad or viewWillLoad. However, some setup may have to be done after the view is loaded and other setup you may want to do before the screen displays anything.
applicationDidFinishLaunching is for initial appwide setup, viewWillLoad is for setup before the view is displayed, and viewDidLoad is for setup right after the view is loaded.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions gets called when your app is finished launching; and viewDidLoad: gets called when an UIView controlled by UIViewController is loaded.

viewDidLoad is the method that is called once the view has been loaded. It is a place where you can insert code that does initial setup of the view once it has been loaded.
The applicationDidFinishLaunching: method of the NSApplication delegate will be called when the app has finished loading.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Method available only in application AppDelegate it calles only ones at the time of app is loaded you can do all the stuff related to your application prelaunch here.
-(void)viewDidLoad: called whenever a view is loaded.
it also call ones when the view is loaded
but it's has own copy for every viewController you can do any stuf related to that controller inside it.

Related

viewDidAppear isn't getting called after running from the background

Method viewDidAppear is not getting called after going from the background back into my iPhone Application.
Does anyone have a solution for this?
I need to call a method every time my MainViewController is shown (even after returning from the background) that will change a label to the new date.
viewDidAppear doesn't seem to be working properly.
Actually it is working properly.
Instead, consider installing a notification handler for UIApplicationDidBecomeActiveNotification or UIApplicationWillEnterForegroundNotification, whichever may be more appropriate for your situation. From there you could do an update of your GUI.
You will have the UIApplicationWillEnterForegroundNotification method in App delegate.Now you need to create the object of the class you want and call the viewwillAppear
this method will be called when the app is in background.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
now for the view will appear code like this
- (void)applicationWillEnterForeground:(UIApplication *)application
{
ViewController *vc = [ViewController alloc]init];//you can use your viewcontroller here
[vc viewDidAppear:YES];// this will call the method.
}
Try using viewWillAppear if you dont mind doing this process before the is actually shown.
Else you can use the method.
- (void)applicationWillEnterForeground:(UIApplication *)application{}

iOS5 with storyboards, where to put common app objects init code?

Before I was using storyboards, all of my controllers were initialized in:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
I could init all the common objects (data managers, etc) before creating controllers, and pass them to controllers.
In my first storyboard project, I noticed that one of my controllers has its
- (void)viewDidLoad
//called before the app's
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
If I"m using storyboards and my controllers get loaded before the app finishes launching, where should I put my common objects init code to ensure that it gets called only once?
Thank you!
From the docs for application:didFinishLaunchingWithOptions:
...It is called after your application has been launched and its main
nib file has been loaded.
To prevent loading your storyboard before your initialization you can remove your main xib file or storyboard in the -Info.plist (for storyboard it is called Main storyboard file base name). Then you can create your storyboard manually when you need that.

Objective C: When to use methods in App Delegate and when to use methods in View Controller

I am a little confused on the following methods in both my View Controller and App delegate classes
Method in App delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Method in ViewController:
- (void)viewDidLoad
Under what situation do I need to add code in the app delegate or ViewController methods? I believe that for switching of views, we need to include it in the app delegate method, are there any rules of thumb that we need to abide by?
Thanks!
Zhen
As it is called at launch,application:didFinishLaunchingWithOptions: typically contains logic to initialise your application (e.g. setting up core data objects, registering for push notifications, etc.). The purpose of viewDidLoad on the other hand is to initialise your view controller before it is shown.
application:didFinishLaunchingWithOptions: should be used for setup that must occur when the application is launched, e.g.
Core Data stack
Restoring application state
Creating application navigation controllers or tab bars
viewDidLoad should be used for any configuration that only needs to be done for that specific view controller. In some cases the view may not get loaded, so there's no point doing that configuration in the app delegate.
e.g
Opening an HTTP connection when the view loads
Asking for location data for a view

Access method in viewcontroller from appdelegate

I want to use appdelegate for applicationDidBecomeActive and I want this function to use method which is placed in viewcontroller. How I am gonna do that?
The only thing that I find is to access from anywhere the appdelegate.
You need to give your applicationDelegate class a reference to the viewController that you want to call the method on. You could do this by creating an instance variable or a property of the applicationDelegate that points to your viewController that you want to be able to call the method on. If you create your viewController in the init method of your appDelegate, or in your applicationDidLoad: method, then you can simply assign this viewController to the instance variable/property that you've created.
You can access the root view controller of your app from within the app delegate through the window property, that is self.window.rootViewController. The view controller that you want to access must be a child of the root view controller or at least accessible through it.
I had the same issue of needing to call a method on my view controller from the app delegate. After some digging and reading this answer I tried to call a method (saveData was the method in question) in my view controller directly from the app delegate and it worked as required.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {[self.window addSubview:rootViewController_iPad.view];
[self.window makeKeyAndVisible];
return YES;
}
-(void)applicationWillResignActive:(UIApplication *)application {
[rootViewController_iPad saveData];
}
Hope this helps.

Calling instance from another view

Can I call the following instance of first view from another second view
(void)applicationDidFinishLaunching:(UIApplication *)application
applicationDidFinishLaunching is called by the application framework when your application launches. You should never be calling it.
It is part of your application's delegate, not of the view.