29

have started a universal project under the new Xcode4. My Application works on both iPhone & iPad devices but not well design for the iPad. I have 2 .xib file for the MainWindow: MainWindow.xib (iPhone device) & MainWindow-Ipad.xib (iPad device). But only one .xib file for my MainView: MainView.xib.

So I'm trying to create a specific .xib file for the iPad Device:

New file > User interface > View -> next -> device family : iPad -> MainView-iPad.xib -> save

And of course, there is nothing connected,no referencing outlet, no link to any class.

I check each thumbnail of my new MainView-iPad.xib in order to link it to my MainViewController class without success !?

Where or how can I link it to my MainViewController class? Thanks in advance.

user763308
  • 435
  • 2
  • 5
  • 10

4 Answers4

93

The correct answer was given by user763308:

To link a .xib to a class under Xcode4:

  1. Open your .xib.
  2. In placeholder: select File's Owner.
  3. In the third thumbnail:"Identity Inspector" of the right panel, edit the "Custom Class" field and enter the name of your class.

I also then had to:

  1. Click the root view under Objects.
  2. Click the last tab: "Show Connections".
  3. Drag "New referencing outlet" to "File's Owner" and select "view".
Viktor Nordling
  • 8,694
  • 4
  • 26
  • 23
  • 3
    This seems like EXACTLY what you'd have to do, and I have had it work for me in the past, but I'm stumped at what to do when this occasionally DOESN'T work when trying to retroactively insert a .xib into a project and the .h/.m class still isn't connected up to the .xib – RanLearns Aug 14 '12 at 02:26
  • It is not auto correcting to the class i need to add in step 3. Tried cleaning the build. – tech savvy Apr 22 '14 at 17:33
  • What do you mean by "click the root view under objects"? – noelicus Oct 08 '18 at 15:13
  • OK - think I know now. You mean click the View square on the left, which is under the "File Owner's" icon/box and the "1st responder" box/icon. Then click the last tab in the right-hand dock called "Show the Connections Inspector". Thanks - didn't work for me :( ... but Tom's answer below sufficed! – noelicus Oct 08 '18 at 15:50
3

You want to create a new UIViewController subclass, not a new UI file. That will create your .xib as well as the class files (.m and .h). The template is under Cocoa Touch.

Wanting to link to an existing view controller is probably not a good idea. If you're following the MVC (Model-View-Controller) design pattern, you have your data classes and view controllers separated. If you can, move things out of your MainViewController class and into a data class it owns. Then create another view controller for iPad and make it own an instance of your data class you created.

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
  • Thank you for Your reply. For sure, I'm trying to follow the MVC Model but for me (and perhaps I'm totally wrong) the .xib files represent the "Views" of the MVC model. I don't see anything wrong to have 1 controller with 2 views.And if I have to create 1 controller for each View, each time I made a change in one of my controller I have to duplicate it in the other controller? In all cases, to link a .xib to a class under Xcode4: *Open your .xib *In placeholder: select File's Owner *In the third thumbnail:"Identity Inspector" of the right panel -custom Class section:choose the class file – user763308 May 21 '11 at 08:47
  • I'm pretty sure you usually create one .xib per UIViewController subclass. Here are the Apple docs on it: https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html#//apple_ref/doc/uid/TP40007457-CH101-SW35 – Tom Hamming May 23 '11 at 17:04
1

To link the .xib to the class file u can do the following steps: -

1) go to the right side view. 2) click on the third button and in that u have a row asking about the custom class der u just write the name of the class file u want it to be linked.

for creating the referencing outlets :-

1) u can copy all the outlets from the iPhone .xib and paste it here and then drag all the outlets to the file owner and then connect them.

Kasaname
  • 1,491
  • 11
  • 15
1

It's not necessarily incorrect to have a 2:1 ratio of .xib to controller class in the case of a Universal App. Here's the pattern I follow:

You have your MainViewController class. You have 2 .xib files: MainView-iPhone.xib and MainView-iPad.xib. Both of these file have their "File's Owner" outlet set to MainViewController. Now, to ensure the view controller actually loads the correct interface and connects all the right outlets depending upon the device on which the app is running, I'll do something like the following:

MainViewController* controller = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    controller = [[MainViewController alloc]initWithNibName:@"MainView-iPad" bundle:[NSBundle mainBundle]];
} else {
    controller = [[MainViewController alloc] initWithNibName:@"MainView-iPhone" bundle:[NSBundle mainBundle]];
}
/* Present the controller */

Now, I should also say that this is only a good idea if there aren't too many distinct code paths that need to be followed when run on different devices. If the controller's behavior is more specialized when running on an iPad vs. and iPhone, it's a better idea to abstract the common behavior in MainViewController and then write two sublasses: MainViewController_iPhone and MainViewController_iPad. Each subclass then loads the appropriate .xib file and handles all of the specifics internally. In this case, the above code would look more like:

MainViewController* controller = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    controller = [[MainViewController_iPad alloc] init];
} else {
    controller = [[MainViewController_iPhone alloc] init];
}
/* Present the controller */
Matt Wilding
  • 20,115
  • 3
  • 67
  • 95