0

我目前正在使用 UISplitView Controller,因为我必须让其中一些在 UITabBarController 中工作。经过几次尝试,我终于找到了一种方便的方法,我得到的唯一问题是我必须手动实例化我的详细信息和主视图,尽管它们已在 IB 中配置并很好地链接。

这是我的做法

我在 MainWindow.xib 中初始化了一个 UITabBarCONtroller 并设置了标签栏项目。

我的第一个选项卡控制器继承自 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 连接在 xib 中,我遇到的问题是,当我的 FirstSplitViewController 从它的 xib 加载时,我的主拆分视图控制器和详细拆分视图控制器未分配(它们在 IB 中链接)。如果我手动分配它们,一切都像一个魅力(在我的 FirstSplitViewController.m 中取消注释 alloc init 行)

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

}

所以我的问题是为什么当 xib 加载时这些对象没有加载?这真的是我第一次必须手动执行此操作。也许我错过了一些东西。

感谢您的任何回答或建议

聪明的

4

1 回答 1

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 的 nil 指针。我认为在可以访问 .xib 引用之前,有必要让控制器添加您的视图。

于 2011-03-21T23:03:27.480 回答