Xcode - How to connect XIB to ViewController Class

Objective CXcodeViewcontroller

Objective C Problem Overview


I created first my TestViewController.h and *.m. Afterwards my TestView.xib.

Now I need to tell my xib: "Yes, please take the class TestViewController as my File's Owner".

I open up my xib, go to the Identity Inspector of its fileOwner and choose under "Custom Class" TestViewController.

But this seems not enough - as when I open up the TestView.xib, and then choose the "Assistent Editor View" it should bring up the corresponding ViewController on the right part of the split screen - in my case the "TestViewController.h". But it doesn't !

Is it necessary to bind the xib in any way to its viewcontroller by dragging lines to files like you do it with outlets and actions?

Objective C Solutions


Solution 1 - Objective C

Click to select the xib. Now, select the file's owner. In the attribute panel on the right side, choose the third tab, "Identity Inspector". There is a header named Custom Class. Give your view controller's name there. After this, you can connect the elements with the file's owner.

enter image description here

Solution 2 - Objective C

I think I ran into exactly this situation when I created a UIViewController subclass but forgot to check "with .xib for UI" when I did it. Later, I went back and created the .xib separately.

Here's a more step-by-step way to associate your new UIViewController and .xib.

  1. Select File's Owner under Placeholders on the left pane of IB. In the Property Inspector(right pane of IB), select the third tab and edit "Class" under "Custom Class" to be the name of your new UIViewController subclass.

  2. Then ctrl-click or right-click on File's Owner in the left pane and draw a line to your top level View in the Objects portion of the left pane. Select the 'view' outlet and you're done.

You should now be able to set up other outlets and actions. You're ready to instantiate your view controller in code and use initWithNibName and your nib name to load it.

Solution 3 - Objective C

In the view controller create a "view" outlet (UIView) and mark it as IBOutlet. (When you use the correct defaults/patterns when creating the files within xcode, then this property should be there already.) In Interface Builder create a link between the main view and the view property/outlet of the view controller/file's owner. Just for the full picture: On creating/allocating the view controller, you should init it with the appropriate XIB file. This is the very moment, where the view controller object is bound to the view that is generated out of the XIB file.

Solution 4 - Objective C

  1. First just like every one said give the view controller name in class of File's Owner

  2. Select File's Owner drag a line from there to view this connects the both

  3. Create an instance of View controller and add it to window to that the code snippet is as follows,

    MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];

  4. Finally add the view controller's view as subview to window.TO do that that the coding is as follows,

    [window addSubview:[controller view]];

Try the following snippet on appdelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after application launch
    [window makeKeyAndVisible];

    MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];
    [window addSubview:[controller view]];
 }

5) Use the following snippet to maximize the size of view to window so that no gape appears

[controller.view setFrame:[[UIScreen mainScreen] applicationFrame]];

Now you will see your view controller as you expected...

I hope this helps....

Solution 5 - Objective C

yes you have to add the view property to file owners of that view controller from interface builder:

Solution 6 - Objective C

choose your fileownr go to identity inspector window, and change class name of file owner to your view .h file, That will connect.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmogioView Question on Stackoverflow
Solution 1 - Objective CAugustineView Answer on Stackoverflow
Solution 2 - Objective CIkeView Answer on Stackoverflow
Solution 3 - Objective CHermann KleckerView Answer on Stackoverflow
Solution 4 - Objective CDurai Amuthan.HView Answer on Stackoverflow
Solution 5 - Objective CAbhishekView Answer on Stackoverflow
Solution 6 - Objective CBittuView Answer on Stackoverflow