0

I'm trying to make my app detect if the user is on a iPhone 5 screen or not.

I'm successfully using the following method in other views.

Via a button I call the Xib / view to be loaded

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

I have two xib's,

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

This ^ goes in the .m file

It should detect if the screen is a iPhone 5 or iPhone 4 screen and adjust the Xib to it.

However, Xcode errors out :

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle /Users/SamGuichelaar/Library/Application Support/iPhone Simulator/6.1/Applications/321B4512-7BD3-46D8-A944-F12029448326/Parkway Drive Gestures.app (loaded)' with name '(null)_568'' First throw call stack:

So, something goes wrong making it not find the original name of the iPhone 4 Xib.

Can anyone help me?

4
  • 1
    It's clear from your exception message that your initWithNibName:bundle: method is being called with nil arguments - hence "(null)_568" Commented Jun 26, 2013 at 21:40
  • But it works on another project perfectly. Any idea how to fix this? Commented Jun 26, 2013 at 21:50
  • I guess you're calling the method manually in that other project? How about here, where and how do you instantiate this view controller? Commented Jun 26, 2013 at 21:51
  • 2
    In your first if condition it looks like you're passing nil as the nib name. What happens if you change it to [[DemoTapeTwoViewController alloc] initWithNibName:@"DemoTapeTwoViewController" bundle:nil]; Commented Jun 26, 2013 at 21:58

1 Answer 1

3

I suggest checking to see if nibName is nil and if so using the class name.

I like to use the ?: for this kind of quick substitution.

- (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; } 

To make it more generic, use 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; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.