PDA

View Full Version : Noob help with UISplitViewController




sparkymark75
Jan 20, 2011, 02:41 PM
Hi,

I'm new to Objective-C (coming from C#) so I'm slowly getting my head around things. Anyway, one thing I'm really struggling with is embedding a TabBar within the DetailsView of a SplitView.

It seems every guide/article I find on this is people asking how to do the opposite, embed a SplitView within a TabBar :rolleyes:

So far I've actually managed to get the TabBar in the DetailsView area of the SplitView however I'm not sure if it's the correct way to do it and there are a couple of issues.

First, I'll describe how I got to where I am;

Create a new app using the built-in XCode SplitView template.
Create a new app using the built-in XCode TabBar template.
Copy the .xib, .h and .m files for the FirstView and SecondView controllers from the TabBar app into the SplitView app.
Discarded the TabBar project as it's no longer required. All other steps are carried out in the SplitView project.
Added the following code to my application delegate header file;
@class RootViewController;
@class DetailViewController;
@class FirstViewController;
@class SecondViewController;

@interface SplitViewTemplateAppDelegate : NSObject <UIApplicationDelegate> {

UIWindow *window;

UISplitViewController *splitViewController;
UITabBarController *tabBarController;

RootViewController *rootViewController;
DetailViewController *detailViewController;
FirstViewController *firstViewController;
SecondViewController *secondViewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UISplitViewController *splitViewController;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet FirstViewController *firstViewController;
@property (nonatomic, retain) IBOutlet SecondViewController *secondViewController;
Opened up MainWindow.xib in IB and changed the class on the DetailsView to UITabController
Added the following code to my application delegate module file;
#import "FirstViewController.h"

@synthesize window, splitViewController, rootViewController, detailViewController, tabBarController, firstViewController, secondViewController;

-(void) makeTabBarController {
NSMutableArray *controllers = [NSMutableArray arrayWithArray:splitViewController.viewControllers];
int index = 0;
for (UIViewController *controller in splitViewController.viewControllers) {
if (index == 1) {

//NSLog(@"Index is: %@", index);
//NSLog(@"Controller name is: %@", controller.title);

UINavigationController *localNavController;
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:2];

firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
localNavController.tabBarItem.title = @"First Tab";
[firstViewController release];

[localViewControllersArray addObject:localNavController];
[localNavController release]; // Retained by above array

secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
localNavController.tabBarItem.title = @"Second Tab";
[secondViewController release];

[localViewControllersArray addObject:localNavController];
[localNavController release]; // Retained by above array

tabBarController.viewControllers = localViewControllersArray;
[localViewControllersArray release]; // Retained thru above setter

//tabBarController.delegate = splitViewController;
[controllers replaceObjectAtIndex:index withObject:tabBarController];
}
index++;
}
splitViewController.viewControllers = controllers;
}
Added [self makeTabBarController]; to the didFinishLaunchingWithOptions method.


So now I get an out of the box SplitView with a tab bar controller on the right with two tabs in it. The tabs work for switching between the views.

A couple of things that I am now struggling with are; The button to fire the Popover is missing, do I need to add this to each tab view?
How do I hook the RootViewController with the TabBarController so that details for the selected item is shown?