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
Related
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.
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 used
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
to get the launchOptions.
But it was not triggered, even if I added the UIApplicationDelegate protocol.
Welcome any comment
Be sure the File's Owner delegate outlet is connected to your App delegate class in the main nib file, unless you want to set it programmatically:
Which iOS version did you try? This works in iOS 3.0 and later.
I recall some odd behavior (this may or may not be true, but something to try) that if you ALSO implement the old method:
- (BOOL)applicationDidFinishLaunchingWithOptions:(UIApplication *)application
Then it will call that instead. Can you confirm you don't have that method?
Fairly new to iPhone app development, so this might be really obvious (if so, apologies in advance!)
I'm building an app which has a tab bar. However, when the app first runs and 'launch screen' is shown with 3 UIButtons - each of these buttons points at a view of one of the tabs. What I need to do is:
Close the existing view
Open the selected view
Set the highlighted tab accordingly
This sounds like it should be quite easy, but a few hours of Googling has found nothing!
Thanks for your help,
Kev
Additional:
Sorry - I am using a tabBarController... But instead of immediately launching the tab bar views I'm using the code below to launch the home menu instead.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
homeViewController *splashView = [[homeViewController alloc] initWithNibName:#"homeView" bundle:nil];
[window addSubview:splashView.view];
// [window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
On the home menu there are UIButtons which need to link to individual tabs... Hope this clarifies...
Cheers!
Oh. Then it gets a bit more complex. What you want to do is basically this:
Add a method to your AppDelegate - (void)showTabBarWithSelectedTab:(NSUInteger)tabIndex. In this method, use tabBarController.selectedIndex to select the correct index, then remove homeViewController's view from the window and add tabBarController's view instead.
In homeViewController, have actions for the buttons that calls the newly created AppDelegate method with the correct tab index.
Generally I would say that this adds a bit too much logic to the AppDelegate. Ideally you'd implement this in a new view controller, surrounding and managing both homeViewController and tabBarController. However, having a UITabbarController inside of another view controller isn't officially supported - although you can get it to work anyway.
it's a bit hard to fully understand your question, but it sounds like you should be using UITabBarController instead of the stand-alone view UITabbar. This is essential reading! Good luck