gpt4 book ai didi

ios - UISplitView Interface Builder nib 对象未分配

转载 作者:行者123 更新时间:2023-11-29 05:06:34 25 4
gpt4 key购买 nike

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

这是我的做法

我在 MainWindow.xib 中初始化 UITabBarCOntroller 并设置选项卡栏项目。

我的第一个选项卡 Controller 继承自 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 Controller 未分配(它们在 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 加载时这些对象没有加载?这确实是我第一次必须手动执行此操作。也许我错过了一些东西。

感谢您的回答或建议

聪明

最佳答案

我刚刚遇到过同样的现象(我认为)。我刚刚开始完全理解 Interface Builder/ Controller 层次结构/ View 层次结构在 iOS 上的工作方式。看起来通过 IBOutlet 链接的成员变量在访问 Controller 实例之前并未初始化。我的代码是这样的:

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

如果我交换了最后两行的位置,除非我重新访问该 View ,否则sectionOneTabController 的指针将为零。我认为在访问 .xib 引用之前,有必要让 Controller 添加您的 View 。

关于ios - UISplitView Interface Builder nib 对象未分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4896344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com