2011-02-04 113 views
0

我目前正在玩UISplitView控制器,因为我必须让它们中的一些在UITabBarController中工作。 经过几次尝试后,我终于找到了一个方便的方法,唯一的问题是我必须手动实例化我的细节和主视图,尽管它们在IB中配置并且链接良好。UISplitView界面生成器未分配的笔尖对象

这里是我如何做到这一点

我在MainWindow.xib中初始化的UITabBarController和设置的TabBar项目。

我的第一个选项卡控制器继承自UISplitViewController并使用xib进行设置。 下面是这个FirstViewController类

#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 

这里的实现是我MasterSplitview实施

#import "MasterSplitViewController.h" 


@implementation MasterSplitViewController 


// 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 

和我DetailSplitViewController的实现

#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在厦门国际银行的挂钩,这个问题我得到的是,当我的FirstSplitViewController从它的xib加载时,我的主控和详细的splitview控制器不是分配(他们在IB连接)。如果我手动ALLOC他们,一切就像一个魅力(下取消注释ALLOC初始化线在我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; 

} 

所以我的问题是,为什么当厦门国际银行是这些对象are't装?这实际上是我第一次手动执行此操作。也许我错过了一些东西。

感谢您的任何答案或建议

S-市场

回答

1

我只是碰到这种同样的现象(我认为)运行。我刚开始完全了解Interface Builder/controller heirarchy/view heirarchy在iOS上的工作方式。它看起来像通过IBOutlet链接的成员变量在访问控制器实例之前不会被初始化。我的代码是这样的:

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]; 

如果我换的最后两行的位置,它会为sectionOneTabController一个零指针,除非我重新审视的看法。我认为在访问.xib引用之前,有一个控制器添加你的视图是必要的。

+0

非常感谢和抱歉,很长一段时间回顾您的答案。看起来像你说过,xib引用需要视图在可访问之前被加载。 – arex 2011-06-04 11:16:06