【发布时间】:2012-04-25 13:21:33
【问题描述】:
我正在 iOS 4.2 及更高版本中创建一个通用应用程序。
我有一个名为DataInputViewController_iPhone.xib 的笔尖。在出现的第一个视图控制器上,我有一个按钮应该启动类DataInputViewController。它可以,但它不显示 XIB 文件的内容。它只是显示一个黑屏。
你知道为什么会这样吗?
我有这段代码调用新的视图控制器:
-(IBAction)clickedButton:(id)sender {
if((UIButton *)sender == (UIButton *)startButton){
NSString *nibName;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
nibName = @"DataInputViewController_iPhone";
} else {
nibName = @"DataInputViewController_iPad";
}
DataInputViewController *nextVC = [[DataInputViewController alloc] initWithNibName:nibName bundle:nil];
[self.navigationController pushViewController:nextVC animated:YES];
}
else if((UIButton *)sender == (UIButton *)otherButton){
NSLog(@"clicked the other button");
}
}
在这个方法和新视图控制器的init 方法中的NSLogging 告诉我nibNameOrNil 变量被正确拉出。
这是DataInputViewController.m 中(标准)init 方法的样子:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
任何想法为什么会发生这种情况?或者如何解决?
谢谢:)
【问题讨论】:
-
你有没有在 DataInputViewController 中实现了 loadView 函数?
-
如果您在 XIB 文件中查找数据输入视图控制器,我猜想有一个 UIView 作为该 XIB 的根元素。检查以确保 File's Owner 视图已连接到该根元素。 (我以为如果是这种情况,您会收到警告或错误,但我可能是错的。按下按钮后,您不会在控制台中收到任何错误或警告,是吗?)
-
转到项目 > 构建阶段 > 复制捆绑资源并确保您的 xib 在那里。
-
@Malek_Jundi 都在那里......奇怪! :)
-
是的,这真的很奇怪,因为我总是使用这种方式,而且效果很好!你能尝试加载 iPad 看看发生了什么吗?