1

I'm trying to make a universal app, so i was wondering if i can connect two nib files ( 1 for iPad and the other for iphone ) to the same .h and .m files ?

i have 3 files TestView.h TestView.m and TestView.xib.... how can i connect a TestView_iPad.xib to the same TestView.h and TestView.m ?

i'm new to Xcode and i'm using Xcode 4 right now

thanx in advance :)

BJ Homer
  • 48,806
  • 11
  • 116
  • 129
aLFaRSi
  • 559
  • 2
  • 12
  • 29

1 Answers1

1

Short answer: Yes.

Provided you follow the model/view/controller style, you can re-use the same View and Viewcontroller (.h and .m) files in both an iPad nib and an iPhone nib (or storyboard). There will be occasions when you need to use the following type of code, though:

BOOL iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);

if (iPad) {
  .... iPad specific code like SplitViewController
} else {
  .... iPhone / iPod Touch specific code
}

You may also need to check if the view controller you are in is on-screen (as on the iPad more than one ViewController can be onscreen), in which case use:

if (self.view.window) {
  .... ViewController onscreen so do something otherwise do nothing
}

Also don't hardcode the bounds of the device's screen. Use the following to find your screen size (in points):

CGRect screenBounds = [[UIScreen mainScreen] bounds];

Hope this helps.

Robotic Cat
  • 5,899
  • 4
  • 41
  • 58
  • Thank you for your reply, sorry i didn't get you, maybe i wasn`t clear, i have 3 files TestView.h TestView.m and TestView.xib.... how can i connect a TestView_iPad.xib to TestView.h and TestView.m ? – aLFaRSi Dec 11 '11 at 03:31
  • 1
    In your iPad storyboard (or nib), go to the Identity inspector and change the class to be your custom class. Same for your iPhone storyboard (or nib). – Robotic Cat Dec 11 '11 at 03:35
  • how can i make a custom class ? – aLFaRSi Dec 11 '11 at 03:44
  • 1
    Not to worry - you've already done it ! The TestView.h and TestView.m files (that work with your iPhone nib) IS your custom class. In IB make sure you have your View Controller selected (not your View) and then in the Identity Inspector click on the little arrow and select your class from the list that appears. – Robotic Cat Dec 11 '11 at 03:48
  • there was no view controller zoo i added it , changed the class to TestView ,, when i start Simulator and click the button to push me to the TestView_ipad.xib the simulator crashes with this error *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "TestView_iPad" nib but the view outlet was not set.' – aLFaRSi Dec 11 '11 at 03:57
  • ` -(IBAction) tstBtn:(id)sender;{ if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { TestView *Test = [[TestView alloc] initWithNibName:nil bundle:nil]; Test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self presentModalViewController:Test animated:YES]; } else{ TestView *Test = [[TestView alloc] initWithNibName:@"TestView_iPad" bundle:nil]; Test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self presentModalViewController:Test animated:YES]; } } ` @Robotic Cat – aLFaRSi Dec 11 '11 at 04:05
  • I am a little surprised that there was no view controller. However, you are now getting on to an entire different issue but the solution to your problem can be found here: http://stackoverflow.com/questions/4763519/loaded-nib-but-the-view-outlet-was-not-set-new-to-interfacebuilder – Robotic Cat Dec 11 '11 at 04:06
  • Thaanks alooot it worked :) ,,, i deleted the view controller ,, selected the files owner the changed it class to TestView, connected the view in the files owner to the view in the left hand side ,,, everything worked :) ,, ,thanks again @Robotic Cat – aLFaRSi Dec 11 '11 at 04:17