60

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?

James Webster
  • 31,873
  • 11
  • 70
  • 114
mogio
  • 843
  • 2
  • 7
  • 18

7 Answers7

94

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

JavaSplice
  • 680
  • 1
  • 5
  • 10
Augustine
  • 1,714
  • 1
  • 17
  • 21
  • You mean I should just select the file in the files menu and then click the "Identity Inspector Tab"? With me that says "no selection". – mogio Jun 27 '12 at 12:12
  • It's because the xib got deselected. Click again on the files owner to activate the identity inspector. – Augustine Jun 27 '12 at 12:25
  • I open up my xib, go to the Identity Inspector of its fileOwner and choose under "Custem 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 ! – mogio Jun 27 '12 at 12:34
  • Sorry I just copied the text from my start entry in here. – mogio Jun 27 '12 at 12:34
  • 1
    That's the best answer – the_marcelo_r Apr 21 '14 at 14:32
  • 1
    This should be selected as the Accepted Answer. This is the best answer. – Erik van der Neut Jun 18 '14 at 11:15
  • Thank you so much! This answer is awesome! Saved me a hell of a lot of time! – ervinbosenbacher Jun 15 '18 at 19:28
  • UIView is not a UIViewController – 6rchid Sep 06 '20 at 00:25
24

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.

Ike
  • 326
  • 2
  • 3
17

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.

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
  • Thanks Hermann, but exactly that is what I am trying to achieve. To do it manually. When I create them automatically the h file just says nothing about the xib. But still there is a connection. I am trying to get this connection without creating instances of properties or methods. I try to understand the mechanism behind the bindings. – mogio Jun 27 '12 at 12:25
  • What exactly do you want to create manually? Do you want to create your full view programmatically without using the Interface Builder and XIB files? – Hermann Klecker Jun 27 '12 at 13:46
  • Or do you want to create the view controller in one place and later create a vew vom XIB and then associate that view with this view controller that exists already? And why? What is it that you want to achieve in the end? – Hermann Klecker Jun 27 '12 at 13:52
  • Thanks Hermann for your help. Eventually I made it in the end. All that was said on this thread was perfectly right. I had to restart xCode and it worked nicely. Weird one ! – mogio Jun 27 '12 at 13:55
  • 6
    You also need to click on File's owner and set it's class to the class of your controller. Then a view outlet will appear in it and you can connect that to the view in your xib – vish Apr 18 '14 at 00:45
6

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....

cjg
  • 2,727
  • 1
  • 19
  • 23
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
0

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

Abhishek
  • 2,255
  • 1
  • 13
  • 21
  • go in interface builder of your TestView after adding the class.. and you will find there files owner in the xib just click on that and see in the property list option check all options you will find there view add to that view with file owners... – Abhishek Jun 27 '12 at 12:15
0

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

Bittu
  • 371
  • 4
  • 17
  • Is that not exactly what I did ? – mogio Jun 27 '12 at 12:09
  • My problem is that xcode is not getting the connection. When I create a ViewController with its Xib in one go it works. But I am not able to connect the viewcontroller to its xib manually. – mogio Jun 27 '12 at 12:14
0

Updated Augustine's answer with Xcode 13.4.1 screenshot.

Follow the steps bellow:

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

enter image description here

Mohammad Daihan
  • 538
  • 4
  • 15