2013-06-26 27 views
0

我试图让我的应用检测用户是否在iPhone 5屏幕上。为iPhone 5加载不同的xib

我在其他视图中成功使用以下方法。

通过按钮我所说的XIB /视图被加载

- (IBAction)DemoTapeTwo:(id)sender { 

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
    DemoTapeTwoViewController *Second = [[DemoTapeTwoViewController alloc] initWithNibName:nil bundle:nil]; 
    [self presentViewController:Second animated:YES completion:NULL]; 

} else { 
    DemoTapeTwoViewController *Second = [[DemoTapeTwoViewController alloc] initWithNibName:@"DemoTapeTwoViewController_iPad" bundle:nil]; 
    [self presentViewController:Second animated:YES completion:NULL]; 


} 

我有两个XIB的,

iPhone 5之一:XViewController_568.xib

iPhone 4之一:XViewController.xib

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle { 
if ([[ UIScreen mainScreen ] bounds ].size.height == 568) { 
    nibName = [NSString stringWithFormat:@"%@_568", nibName]; 
} 
if (self = [super initWithNibName:nibName bundle:nibBundle]) { 

} 
return self; 
} 

这^进入.m文件

它应该检测屏幕是否是iPhone 5或iPhone 4屏幕并调整Xib。

然而,Xcode中出现了错误:由于未捕获的异常 'NSInternalInconsistencyException'

终止应用程序, 原因: '不能在包中加载NIB:' 一个NSBundle /用户/ SamGuichelaar /库/应用程序支持/ iPhone模拟器/ 6.1 /应用/ 321B4512-7BD3-46D8-A944-F12029448326 /大路驱动Gestures.app(加载) '名为'(空)_568' 第一掷调用堆栈:

所以,不顺心的事变得无法找到iPhone 4 Xib的原始名称。

任何人都可以帮助我吗?

+1

这是一个从你的异常信息清楚地表明你的initWithNibName:包:方法被调用无参数 - 因此,“(空)_568” –

+0

但是它适用于其他项目完美。任何想法如何解决这个问题? –

+0

我想你在其他项目中手动调用该方法?这里怎么样,你在哪里以及如何实例化这个视图控制器? –

回答

3

我建议检查nibName是否为零,如果是的话,使用类名称。

我喜欢使用?:进行这种快速替换。

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle { 
    if ([[ UIScreen mainScreen ] bounds ].size.height == 568) { 
     nibName = [NSString stringWithFormat:@"%@_568", nibName ?: @"DemoTapeTwoViewController"]; 
    } 

    if (self = [super initWithNibName:nibName bundle:nibBundle]) { 

    } 
    return self; 
} 

为了使它更通用的使用NSStringFromClass()

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle { 
    if ([[ UIScreen mainScreen ] bounds ].size.height == 568) { 
     nibName = [NSString stringWithFormat:@"%@_568", nibName ?: NSStringFromClass([self class])]; 
    } 

    if (self = [super initWithNibName:nibName bundle:nibBundle]) { 

    } 
    return self; 
} 
+0

谢谢,这为我修好了。 :) –