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.
Related
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.
I am moving from theory to some practice. I've downloaded from Apple site a couple of sample codes. The first app is TableViewSuite from
https://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html
Looks nice and attractive. The most thing I like is mastering .nib file programmatically. I tried to repeat this app, but oh Dear, what kind of project to choose?
Navigation-Based Application
View-Based Application
or
Window-Based Application?
First I tried Window-Based Application cos it promises
This template provides a starting point for any application. It provides just an application delegate and a window.
Sounds good. Just window and delegate, but when I started to write code I've faced such dilemma. In Apple's code, the first thing I have to implement for exposing nib file with table view is
- (void)applicationDidFinishLaunching:(UIApplication *)application {
/*
Create and configure the navigation and view controllers.
*/
RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
// Retrieve the array of known time zone names, then sort the array and pass it to the root view controller.
NSArray *timeZones = [NSTimeZone knownTimeZoneNames];
rootViewController.timeZoneNames = [timeZones sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
[aNavigationController release];
[rootViewController release];
// Configure and display the window.
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
This method is clear for me. I mean it's clear for me what it does. In my app this method is implemented in quite different way.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
It returns BOOL instead of returning void and doesn't get (UIApplication *)application parameter and I can't initialize RootViewController with style.
So, what kind of project should I choose? Please help me with your advice. Thanx in advance.
Hey nathan both these method does the same.
And if you are missing application instance then you can create it using [UIApplication sharedApplication] as this is a singleton and will return the same instance every time.
If you are new to iPhone then go for View Based first then go for Navigation based app and then finally for window based application.
And about the two method above - (void)applicationDidFinishLaunching:(UIApplication *)application method is used in earlier versions of iOS to initialize the application and prepare it to run. In iOS 3.0 and later, you should use the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions instead.
These are the lines straight from apple's docs you can check here
The difference between these two method is that when your applicaion is launched due to local/push notification the the method - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions is called and a dictionary is passed as launch option. So use this one instead of other.
And about the above downloaded code it is a navigation based application.
As far as what type of project to use, I think choosing a Window based application is a good starting point. As you said it's a window and a delegate and those are pretty much the essentials.
This method:
(void)applicationDidFinishLaunching:(UIApplication *)application
is actually used in older versions of iOS. You should be using:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
This method basically does the same thing except it takes two parameters instead of 1. The first is the UIApplication (just like the first method). The 2nd parameter is a dictionary the tells you why the application was launched (opened from springboard, opened as a result of acting on a push notification, etc).
As for the return value, you probably want to return NO when just starting out. If your app is going to handle URL resources then you will probably want to re-implement it so that it looks inside the options dictionary to see if the app is launching because the user is trying to open a file or resource that your application claims to support (in which case you would return YES).
There should be no reason the code you posted above cannot be used. You just need to add a return value. There shouldn't be anything stopping you from initializing your UINavigationController or your RootViewController (which I assume is a subclass of UITableView).
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
I have a problem in adapting my iPhone App to Universal.
In my iPhone App, I have a tabBarController, with 5 tabs, each one with a tableView.
I need now to adapt it to iPad, so I'm implementing the following:
. A UISpliViewController, in which the rootViewController (left pane) is a tableView, to display in detailViewController (on the right side), each controller, corresponding to the tarBarController on the iPhone.
So, my problem is where do I assign the controllers to the splitView? In AppDelegate?
If I assign them in viewDidLoad on rootViewController, it don't work.
Anyone can help me? I'm stuck.
Thanks,
iChat: rui.lopes#me.com
create your splitview controller either in the xib or programmatically, then set the viewcontrollers
splitViewController.viewControllers = [NSArray arrayWithObjects:leftViewController, rightViewController, nil];
in either method
- (void)applicationDidBecomeActive:(UIApplication *)application or
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
In the AppDelegate's:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method.
EDIT: Also see this SO post. Had bookmarked this when I started out.
UISplitViewController programmtically without nib/xib
I've done it.
I've added 5 ViewControllers to the AppDelegate, and the problem is solved.
Thanks,
Rui
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.