Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link a .xib file to a class file with Xcode 4

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.

like image 422
user763308 Avatar asked May 20 '11 19:05

user763308


4 Answers

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".
like image 185
Viktor Nordling Avatar answered Nov 19 '22 14:11

Viktor Nordling


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.

like image 38
Tom Hamming Avatar answered Nov 19 '22 13:11

Tom Hamming


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 */
like image 1
Matt Wilding Avatar answered Nov 19 '22 13:11

Matt Wilding


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.

like image 1
Kasaname Avatar answered Nov 19 '22 12:11

Kasaname