无法为Xcode 4.5中的Single ViewController类加载两个nib(.XIB)

对不起先生,我知道已经有很多类似的问题了。 我尝试了所有的解决scheme,但没有为我工作。

我正在使用Xcode 4.5.2和iPhone5 / ios6使用两个xib 1> RootViewController5和所有其他设备2> RootViewController这两个nib文件有单个ViewController命名RootViewController 。在这两个nib文件的文件所有者我已经select了RootViewController类定制class级督察。

现在在ViewDidLoad方法中,我试图加载这样的两个笔尖

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { CGSize result = [[UIScreen mainScreen] bounds].size; UIViewController *viewController3; if(result.height == 480) { viewController3 = [[[UIViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease]; } if(result.height == 568) { viewController3 = [[[UIViewController alloc] initWithNibName:@"RootViewController5" bundle:nil] autorelease]; NSLog(@"iphone 5 123"); } } 

我也尝试了下面的代码

  if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { CGSize result = [[UIScreen mainScreen] bounds].size; RootViewController *viewController3; if(result.height == 480) { viewController3 = [[[UIViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease]; } if(result.height == 568) { viewController3 = [[[UIViewController alloc] initWithNibName:@"RootViewController5" bundle:nil] autorelease]; NSLog(@"iphone 5 123"); } } 

但没有运气。 请告诉我哪里错了。

谢谢

MAYUR

我build议你做这样的事情,而不是:

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){ if([UIScreen mainScreen].bounds.size.height == 568.0) { //Use iPhone5 VC self = [super initWithNibName:@"RootViewController-568h" bundle:nibBundleOrNil]; } else{ //Use Default VC self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; } } return self; } 

那就是如果你的RootViewController就是这样命名的。 通过这样做,如果他们决定添加另一个尺寸的iPhone / iPod,可以避免将来的崩溃。 正如你使用两个if语句,如果两个都不是真的,它会崩溃的应用程序,真的不是好的编码。

一个好的做法是,总是试图提前思考,并为未来做出规划,如果他们应该发布另一个屏幕尺寸,它不会看起来不错,但至less不会崩溃。

我从你的描述中看到的显而易见的问题是,你在自己的笔尖中设置了自定义的类“RootViewController”,但是实际上在你的代码中实例化了一个“UIViewController”。

你应该做的是:

 viewController3 = [[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease]; 

否则,当运行时加载你的笔尖,并且试图在你的笔尖上设置那些RootViewController特定的sockets时,运行时将无法在香草UIViewController中find那些,并因此崩溃。

我不认为你必须为iPhone5和iPhone4,4S等使用两个不同的.xib。如果你想改变图像,标签,button等的大小,这就是为什么使用Spring和Structs。 您也可以通过使用您在问题中编写的代码(在.m文件中)以编程方式设置大小…….

我之前也犯过这个错误。 当我用来运行程序时,我总是想出错误

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "View" nib but the view outlet was not set.' *** First throw call stack:(0x1c93012 0x10d0e7e 0x1c92deb 0xf58c8 0xf5dc8 0xf5ff8 0xf6232 0x453d5 0x4576f 0x45905 0x4e917 0x2b7f 0x12157 0x12747 0x1394b 0x24cb5 0x25beb 0x17698 0x1beedf9 0x1beead0 0x1c08bf5 0x1c08962 0x1c39bb6 0x1c38f44 0x1c38e1b 0x1317a 0x14ffc 0x25fd 0x2525 0x1)libc++abi.dylib: terminate called throwing an exception 

这是因为“笔尖没有设置”

所以,我认为你应该使用弹簧和结构或者以编程的方式进行。

我会build议你:

使用故事板,而不是实例化VC时的XIB文件,例如:

  myVC* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"myVCStoryBoardID"]; [self.navigationController pushViewController:vc animated:YES]; 

这有助于将VCdevise保持在同一屋檐下,并使用故事板的强大function。

然后检查故事板控制器上的“自动布局”是否处于活动状态,并确保标签上的约束映射到其他元素(非常重要)的边界上方和下方。 当你移动它时,用蓝色虚线表示。 在大多数情况下,无论屏幕高度如何,运行时都可以alignment所有内容。

我知道在这方面可能会有一些讨厌的情况,所以你可能不得不手动调整东西。 除非有复杂的graphics,否则总是可以在[0,1]区间内使用Y坐标,一旦必须设置帧,请使用[[UIScreen mainScreen]边界] .size获取适当的值,四舍五入到最近的整数。

如果以上所有失败,那么你可能不得不创build单独的VC,但在我看来,这不是真正的SDK的目的。

祝你好运!

哇,我真是个傻瓜,我没有意识到我没有正确加载笔尖。 只有我错了是这行代码

我写了viewController3 = [[[UIViewController alloc] initWithNibName:@"RootViewController5" bundle:nil] autorelease]; 代替

  [[NSBundle mainBundle] loadNibNamed:@"RootViewController5" owner:self options:nil]; 

所以我的最终代码看起来像这样,工作绝对好

 if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { CGSize result = [[UIScreen mainScreen] bounds].size; if(result.height == 480) { // iPhone Classic [[NSBundle mainBundle] loadNibNamed:@"RootViewController" owner:self options:nil]; } if(result.height == 568) { // iPhone 5 [[NSBundle mainBundle] loadNibNamed:@"RootViewController5" owner:self options:nil]; } } 

感谢这个链接链接到正确答案