0

UITabBarControllerで動作させる必要があるため、現在UISplitViewControllerで遊んでいます。数回試した後、私はついにそれを行うための便利な方法を見つけました。私が得る唯一の問題は、詳細とマスタービューが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からロードされるときに、マスターと詳細のsplitviewコントローラーが割り当てられない(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

私はちょうどこの同じ現象に遭遇しました(私は思います)。私は、InterfaceBuilder/コントローラー階層/ビュー階層が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];

最後の2行の場所を入れ替えた場合、ビューに再度アクセスしない限り、sectionOneTabControllerへのポインターはnilになります。.xib参照にアクセスする前に、コントローラーにビューを追加してもらう必要があると思います。

于 2011-03-21T23:03:27.480 に答える