In the iPad SplitView template, where's the code that specifies which views are to be used in the SplitView? - xcode

In the iPad Programming Guide, it gives the following code example for specifying the two views (firstVC and secondVC) that will be used in the SplitView...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MyFirstViewController* firstVC = [[[MyFirstViewController alloc]
initWithNibName:#"FirstNib" bundle:nil] autorelease];
MySecondViewController* secondVC = [[[MySecondViewController alloc]
initWithNibName:#"SecondNib" bundle:nil] autorelease];
UISplitViewController* splitVC = [[UISplitViewController alloc] init];
splitVC.viewControllers = [NSArray arrayWithObjects:firstVC, secondVC, nil];
[window addSubview:splitVC.view];
[window makeKeyAndVisible];
return YES;
}
but when I actually create a new SplitView project in Xcode, I don't see any code that says the default rootView and detailView views should be added to the SplitView. Where can I find that?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch
rootViewController.managedObjectContext = self.managedObjectContext;
// Add the split view controller's view to the window and display.
[window addSubview:splitViewController.view];
[window makeKeyAndVisible];
return YES;
}
I'm new to iPhone OS programming and I'm just trying to understand how this all works. Thanks in advance for all your help! I'm going to continue researching this question right now.

It's because the links are set up in the *.nib file already. You could still use the .viewControllers approach if you don't want to rely on the *.nib to do it automatically.

Related

How to use 2 storyboards for different screen sizes?

What and where do I put code for two storyboards one if they are running on a 3.5" screen and one for a 4" screen. I have two storyboards. One is named Main.storyboard and the other one is named Main2.storyboard.
The code goes into the AppDelegate.m file and looks similar to this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:#"AlternateStoryboard" bundle:nil];
UIViewController *vc = [mainstoryboard instantiateInitialViewController];
self.window.rootViewController = vc;
return YES;
}
You'll need to check the screen size first, of course, see the docs for UIScreen.
Something like this might help. In your AppDelegate's application:didFinishLaunchingWithOptions: method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone
&& [UIScreen mainScreen].bounds.size.height == 568.0) {
//is 4 inch screen
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController = [storyboard instantiateInitialViewController];
self.window.rootViewController = viewController;
}
else {
//is 3.5 inch screen
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main2" bundle:nil];
UIViewController *viewController = [storyboard instantiateInitialViewController];
self.window.rootViewController = viewController;
}
return YES;
}
The if/else statement checks the screen size and loads the appropriate storyboard.

appdelegate object in xib

I am trying to follow the apple photopicker example which has an appdelegate object in an .xib file. How does an appdelegate object get into a xib file, and why is it important?
In my app I want a navigation controller like the photopicker example has. My test work has not included a navigation controller and I am having difficulty inserting one into my test work now that it has just a view controller because the interface builder seems to have made up its mind to not allow the addition. Will it be easiest for me to just start over with a new app that has the navigation controller architecture and then add in copies of my test work methods and classes. Or is starting over unnecessary? I suspect that inserting the appdelegate object into the xib would allow me to use the exist test work more directly.
update 0
I think I followed the instructions in your answer. Xcode suggested _viewController and I changed that. My app is for both iPad and iPhone (and I am working only on iPad now), so it is a little more complicated, I think. I compile fine, but I still see no appdelegate object in my xib file even though PhotoPicker's xib file has one.
I suspect I need to hook up some objects still, but I could use some help with that, too. In PhotoPicker the Connections Inspector for the Navigation Controller says that the Referencing Outlet is between navController and AppDelegate, and the Connections Inspector for the AppDelegate says that the Referencing Outlet is between delegate and File's Owner. But because my xib file has no (BS)AppDelegate object, I don't know how to hook up without it.
#interface BSAppDelegate ()
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[BSViewController alloc] initWithNibName:#"BSViewController_iPhone" bundle:nil];
} else {
self.viewController = [[BSViewController alloc] initWithNibName:#"BSViewController_iPad" bundle:nil];
}
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
update 0
Inside (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions, you can add uinavigationcontroller like this:
UIViewController *viewController = … //load your first view controller with the initial xib here
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navigationController;

Xcode Transitioning from .xib to Storyboards

I have a sample xcode project which I downloaded to test out this. It has 2 .xib files (each with their own view controller header and mains). I have added a storyboard and set the ViewControllers classes to game and ViewController (the names of the .xibs) and set the main storyboard to the storyboard in the summary, but whenever I run the app it just runs the .xibs.
How can I get it to run the storyboard instead of the .xibs?
Is there a section in storyboards I need to change or is it in the code for each .xibs .h and .m?
Thanks for the help
Take a look in your App Delegate. Here is an example set up to run with XIBS or Storyboard depending on target conditions...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#ifdef FORMAT_XIB
NSLog (#"XIB VERSION");
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CVViewController* myViewController = nil;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
myViewController = [ [CVViewController alloc] initWithNibName:#"CVViewController_iPhone" bundle:nil ];
} else {
myViewController = [ [CVViewController alloc] initWithNibName:#"CVViewController_iPad" bundle:nil ];
}
self.window.rootViewController = myViewController;
[self.window makeKeyAndVisible];
#else
NSLog (#"STORYBOARD VERSION");
#endif
return YES;
}
If you are running a Storyboard app, you don't need all that. Delete it so that the application didFinishLaunchingWithOptions: method just reads:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}

how to custom navigation bar in ios 5 and ios 6?

As I saw when I searched here it was a lot of questions on the same topic, but I really dont know which codes to use and where to put them.
I simply want to change the navigation bar to a custom bar with my own background image.
I did see some codes, but I am really new so I dont know how to do this!
Like this
-Simon
Use the appearance property
[[UINavigationBar appearance] setBackgroundImage:yourimage forBarMetrics:UIBarMetricsDefault];
//first put this code in AppDelegate.m
in this method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//code: navigationbar set image and fontcolor:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"your_navigationimg_bg"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes: #{
UITextAttributeTextShadowColor: [UIColor clearColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:#"AppleGothic" size:20.0f]
}];
[self.window makeKeyAndVisible];
return YES;
}
//second: put this code in RootViewController.m
in this method:
- (void)viewDidLoad
{
self.navigationItem.title=#"your title";
[super viewDidLoad];
}

UITabBarController + UINavigationController problem xcode project

I have a problem, I have created a project window based application in xcode, then I create a UITabBarController that manages two views all programmatically, the second view is a tableView and I want to see in the top a UINavigationController, I have tried a lot but I don't know how to have a UINavigationController in the second view. this is the code:
ProjectAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Creo una tabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
//Create the two view controllers
UIViewController *vc1 = [[Visuale1ViewController alloc] init];
UIViewController *vc2 = [[Visuale2ViewController alloc] init];
//Make an array containing the two view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];
//The viewControllers array retains vc1 and vc2, we can release
//our ownership of them in this method
[vc1 release];
[vc2 release];
//Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers];
//Setto la tabBarController come rootViewController di window
[window setRootViewController:tabBarController];
//The window retain tabBarController, possiamo lasciare il nostro riferimento
[tabBarController release];
[self.window makeKeyAndVisible];
return YES;
}
Visuale1ViewController.h
#implementation Visuale1ViewController
- (id)init{
[super initWithNibName:#"Visuale1ViewController" bundle:nil];
//Get the tab bar item
UITabBarItem *tbi = [self tabBarItem];
//Give it a label
[tbi setTitle:#"Visuale 1"];
return self;
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle {
return [self init];
}
Visuale2ViewController.h
#implementation AnswerViewController
- (id)init{
//Call the superclass's designated initializer
/*[super initWithNibName:nil
bundle:nil];*/
[super initWithStyle:UITableViewStyleGrouped];
answers = [[NSMutableArray alloc] init];
for (int i = 0; i<10 ; i++) {
[answers addObject:[Answer DefaultAnswer]];
}
//Get the tab bar item
UITabBarItem *tbi = [self tabBarItem];
//Give it a laber
[tbi setTitle:#"Visuale 2"];
return self;
}
- (id)initWithStyle:(UITableViewStyle)style{
return [self init];
}
//All below are all methods to work the table view, and all go well, the only problem it's the UINavigationController, to manage then the detail of the table...
Now I want to know how I can put a UINavigationController in the second view. I try do this, in ProjectAppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Creo una tabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
//Create the two view controllers
UIViewController *vc1 = [[Visuale1ViewController alloc] init];
UIViewController *vc2 = [[Visuale2ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc2];
//Make an array containing the two view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, navController, nil];
//The viewControllers array retains vc1 and vc2, we can release
//our ownership of them in this method
[vc1 release];
[vc2 release];
//Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers];
//Setto la tabBarController come rootViewController di window
[window setRootViewController:tabBarController];
}
In this way I can visualize the NavigationBar, but I lost the name of the SecondTabBar. Sorry for my english, how I can do this?
Yes in the second view you have to set title as
[self.navigationItem setTitle:#"Visuale2"];
For TabBar title-
UITabBar *tabBar = [self.tabBarController tabBar];
NSArray *tabBarItems = [tabBar items];
UITabBarItem *secondTabBarItem = [tabBarItems objectAtIndex:1];
[secondTabBarItem setTitle:#"Visuale2"];
I left the code as it was and I added in init Visale2ViewController this:
UIImage *i = [UIImage imageNamed:#"Hypno.png"];
[tbi setImage:i];
and now in can see the text Visuale2 in tabBar and the image...i don't know why...
You need to set the tabBarItem property for your UINavigationController. Something like this.
UITabBarItem *tabItem = [[UITabBarItem alloc] initWithTitle:#"Visuale 2" image:nil tag:1];
UIViewController *vc2 = [[Visuale2ViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:vc2];
navController.tabBarItem = tabItem;
After looking at it,the scenario seems to be same like me.What I faced for the first time when doing Tab+Navigation.
I am sure that there is some problem with your Tab+Navigation based application. Although it shows the Tab as well as navigation are not able to navigate the basic flow.And it is very difficult to solve your problem with such less code.
Instead of this, I had an alternate solution for the same:
Once you have a tab bar in a XIB, the easiest way to approach this is to drag a UINavigationController object over from the Library window (looks like a left nav bar button on a gold background) into the Tree View for your tab bar (the text only view, not the GUI). Place it under the tab bar, then drag your existing view controller under the tab bar controller instead of under the tab bar.
When you go to view that tab you should then see a navigation bar on the top of it... if you are loading the navigation controller from another xib, you'll modify the nav bar in the tab bar xib.
else you can below you can follow the best url for the same:
http://books.google.co.in/books?id=2yYlm_2ktFYC&pg=PA179&lpg=PA179&dq=navigation+with+the+tab+based+application+iphoneSDK&source=bl&ots=nf2YYjX5Am&sig=COpHj9wOtsDChQBglpsljSTsElw&hl=en&ei=3ZoFTeGSOI_tsgbc_Iz6CQ&sa=X&oi=book_result&ct=result&resnum=6&ved=0CDAQ6AEwBQ#v=onepage&q&f=false
http://www.youtube.com/watch?v=LBnPfAtswgw
Hope this will surely solve your problem.

Resources