I'm currently playing with UISplitView Controller as I've got to have some of them working in a UITabBarController. After a few tries, I've finally found a convenient way od doing it, the only issue I get is that I have to manually instanciate my detail and master view althought they are configured in IB and linked well.
Here is how I do it
I initialize a UITabBarCOntroller in my MainWindow.xib and set the tabbar items.
My first tab controller inherits from UISplitViewController and is set up with a xib. Here is the implementation of this FirstViewController class
#import "FirstSplitViewController.h"
#import "MasterSplitViewController.h"
#import "DetailSplitViewController.h"
@implementation FirstSplitViewController
@synthesize detailSplitViewController,masterSplitViewController;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
/*
masterSplitViewController = [[[MasterSplitViewController alloc] initWithNibName:@"MasterSplitViewController" bundle:nil] autorelease];
detailSplitViewController = [[[DetailSplitViewController alloc] initWithNibName:@"DetailSplitViewController" bundle:nil] autorelease];
*/
self.viewControllers = [NSArray arrayWithObjects:masterSplitViewController, detailSplitViewController , nil];
self.delegate = detailSplitViewController;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
Here is my MasterSplitview Implementation
#import "MasterSplitViewController.h"
@implementation MasterSplitVi开发者_如何学编程ewController
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
and my DetailSplitViewController's implementation
#import "DetailSplitViewController.h"
@interface DetailSplitViewController ()
@property (nonatomic, retain) UIPopoverController *popoverController;
- (void)configureView;
@end
@implementation DetailSplitViewController
@synthesize toolbar, popoverController, detailItem, detailDescriptionLabel;
/*
When setting the detail item, update the view and dismiss the popover controller if it's showing.
*/
- (void)setDetailItem:(id)newDetailItem {
if (detailItem != newDetailItem) {
[detailItem release];
detailItem = [newDetailItem retain];
// Update the view.
[self configureView];
}
if (self.popoverController != nil) {
[self.popoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView {
// Update the user interface for the detail item.
// detailDescriptionLabel.text = [detailItem description];
}
- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc
{
barButtonItem.title = @"Root List";
NSMutableArray *items = [[toolbar items] mutableCopy];
[items insertObject:barButtonItem atIndex:0];
[toolbar setItems:items animated:YES];
[items release];
self.popoverController = pc;
}
// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
NSMutableArray *items = [[toolbar items] mutableCopy];
[items removeObjectAtIndex:0];
[toolbar setItems:items animated:YES];
[items release];
self.popoverController = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
//[toolbar release];
[super dealloc];
}
@end
Everiting is hooked up in xib's, and the problem I get is that when my FirstSplitViewController is loaded from it's xib, my master and detail splitview controllers aren't allocated (they are linked in IB). If I alloc them manually, everything works like a charm (uncommenting alloc init lines below in my FirstSplitViewController.m)
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
/*
masterSplitViewController = [[[MasterSplitViewController alloc] initWithNibName:@"MasterSplitViewController" bundle:nil] autorelease];
detailSplitViewController = [[[DetailSplitViewController alloc] initWithNibName:@"DetailSplitViewController" bundle:nil] autorelease];
*/
self.viewControllers = [NSArray arrayWithObjects:masterSplitViewController, detailSplitViewController , nil];
self.delegate = detailSplitViewController;
}
So my question is why those objects are't loaded when the xib is? This is really the first time I have to do this manually. Maybe I'm missing something.
Thanks for any answers or advice
s-mart
I've just run across this same phenomenon (I think). I'm just beginning to fully understand the way that Interface Builder/controller heirarchy/view heirarchy works for iOS. It looks like the member variables that are linked via IBOutlet are not initialized until the controller instance is accessed. My code was thus:
if(self.sectionOneViewController == nil)
{
SectionOneViewController *sectionOneView = [[SectionOneViewController alloc]
initWithNibName:@"SectionOne"
bundle:[NSBundle mainBundle]];
self.sectionOneViewController = sectionOneView;
[sectionOneView release];
//[self showSectionOne:sender];
}
[self.navigationController pushViewController:self.sectionOneViewController animated:YES];
[[UIApplication sharedApplication].keyWindow addSubview:self.sectionOneViewController.sectionOneTabController.view];
if I swapped the location of the last 2 lines it would have a nil pointer for the sectionOneTabController unless I revisited the view. I think having a controller add your view is necessary before the .xib references can be accessed.
精彩评论