我正在尝试让我的应用检测用户是否在iPhone 5屏幕上。
我在其他视图中成功使用了以下方法。
通过按钮我调用要加载的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 one:XViewController_568.xib
iPhone 4 one: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 / Users / SamGuichelaar / Library / Application Support / iPhone Simulator / 6.1 / Applications / 321B4512-7BD3-46D8-A944-F12029448326 / Parkway Drive Gestures.app(loaded)'的名称'(零)_568'” 第一次抛出调用堆栈:
因此,出现问题导致它找不到iPhone 4 Xib的原始名称。
任何人都可以帮助我吗?
答案 0 :(得分:3)
我建议检查nibName是否为nil,如果是,则使用类名。
我喜欢使用?:
进行这种快速替换。
- (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;
}