Xcode - How to connect XIB to ViewController Class
Asked Answered
Y

7

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?

Yep answered 27/6, 2012 at 11:57 Comment(0)
D
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

Danford answered 27/6, 2012 at 12:9 Comment(8)
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".Yep
It's because the xib got deselected. Click again on the files owner to activate the identity inspector.Danford
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 !Yep
Sorry I just copied the text from my start entry in here.Yep
That's the best answerStar
This should be selected as the Accepted Answer. This is the best answer.Venose
Thank you so much! This answer is awesome! Saved me a hell of a lot of time!Broiler
UIView is not a UIViewControllerNoheminoil
T
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.

Twinkle answered 1/8, 2013 at 20:30 Comment(1)
frick. there's no "with .xib for UI" in the latest version of xcode....Leper
G
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.

Gwenette answered 27/6, 2012 at 12:11 Comment(5)
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.Yep
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?Gwenette
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?Gwenette
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 !Yep
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 xibCammiecammy
P
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....

Pharynx answered 5/4, 2013 at 4:51 Comment(0)
I
0

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

Inchoation answered 27/6, 2012 at 12:3 Comment(1)
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...Inchoation
B
0

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

Billi answered 27/6, 2012 at 12:7 Comment(2)
Is that not exactly what I did ?Yep
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.Yep
C
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

Cassatt answered 21/9, 2022 at 4:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.