Xcode - How to connect XIB to ViewController Class
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?
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.
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.
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.
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.
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.
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....