Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create view controller that is defined in XIB?

Sorry for perhaps noob question, guys. I'm new to iOS development.

How can I create view controller that is defined in XIB ?

When you create new class inherited from UIViewController in Xcode via "New File" and mark with XIB checkbox it creates xib file that doesn't have viewController element. But rather it has placeholder which points to UIViewController which is defined in the code.

But I see that you can create controller itself in Interface Builder and specify customClass for it.

Unfortunately it doesn't work for me. I get loaded the "EmbeddedViewController" nib but the view outlet was not set error in run-time. But I believed view property must be set by resource loader automatically since everything is defined via XIB. It works that way for TableView and its cells for example.

Could you please provide example how to do it ?

I created sample project: https://github.com/cppexpert/SampleWithNib

The controller in question is EmbeddedViewController

like image 614
expert Avatar asked Aug 30 '13 01:08

expert


People also ask

How do I create a new view controller?

To create a new view controller, select File->New->File and select a Cocoa Touch Class. Choose whether to create it with Swift or Objective-C and inherit from UIViewController . Don't create it with a xib (a separate Interface Builder file), as you will most likely add it to an existing storyboard.


2 Answers

Create one Xib per UIViewController

Each Xib have File's Owner object for you to set UIViewController class there. Click File's Owner and choose EmbeddedViewController on it's class

Then drag main UIView not UIViewController class to there, then hook up this view with file's owner as view. UIViewController just use to drag to StoryBoard base project.

https://github.com/lequysang/gitfiles02/blob/master/SampleWithNib-master.zip

like image 158
LE SANG Avatar answered Oct 06 '22 00:10

LE SANG


Turned out these controls exist in IB for Storyboard projects.

like image 39
expert Avatar answered Oct 05 '22 22:10

expert