【问题标题】:UISplitView Interface Builder nib objects not allocatedUISplitView Interface Builder nib 对象未分配
【发布时间】:2011-06-21 05:56:02
【问题描述】:

我目前正在使用 UISplitView 控制器,因为我必须让其中一些在 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 加载时这些对象没有加载?这真的是我第一次必须手动执行此操作。也许我错过了什么。

感谢您的任何回答或建议

智能超市

【问题讨论】:

    标签: ios uisplitviewcontroller nib xib iboutlet


    【解决方案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 引用之前,有必要让控制器添加您的视图。

    【讨论】:

    • 非常感谢,很抱歉长时间查看您的答案。好像您说 xib 引用需要在可访问之前加载视图。
    猜你喜欢
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多