christianleroy
christianleroy

Reputation: 1104

Does every .xib file have to have its own .h and .m files?

I'm researching about iOS development because I'm about to start creating an iOS app using web service in a few days. Although I've already read and seen a lot of tutorials, documentations, forums and whatnot, the question I have is still unanswered, or rather the answer is not clearly stated. I hope someone can help me. I was just wondering about this because if every .xib file has to have its own class files, then it would mean I'd have a lot of files.

Upvotes: 2

Views: 966

Answers (3)

Myron Slaw
Myron Slaw

Reputation: 886

The answer is no, but when using nib files for each view controller (instead of a storyboard which is a collection of nib files) you usually do.

nib files archive object graphs

Remember this. Interface builder, and the nib files it creates, is used to create object graphs. You could actually throw arbitrary objects in a nib, and load them up at runtime. When I was first getting started, this was one of the most confusing topics.

xib files (I still call them nibs) Are just a description of an object graph which you can build visually. Basically, every object you set up in a nib is archived and at runtime, is unarchived. The settings you chose in interface builder are also archived, and are the equivalent of calling a method in code.

For example, selecting a background color for a UIView in a nib is the same as sending setBackgroundColor: in code. Resizing a view is like sending setFrame: etc. This is all archived when you build, and unarchived at runtime.

nib files keep references to objects that you subclass.

If you have a custom view, you'll have a subclass of that (.h and .m) and set it up in a nib. Unless of course you don't want the view to do anything.

Setting the class here sets a reference to your .h file and allows you to set up common settings in IB

Usually, when you create a nib file, on iOS anyway, you opt in to get a .h and .m file for a view controller subclass with it. This ends up being connected to the files owner proxy object in the nib. Using UIView as an example, when you create a subclass for a view, setting it in interface builder gives you a reference to that code. You can then set its size, background color, drawing options etc in the nib and what it actually draws in code.

Options in interface builder which are archived at runtime.  This saves you time and lines of code

Here is a detailed article from apple about nibs:

https://developer.apple.com/library/ios/documentation/cocoa/conceptual/LoadingResources/CocoaNibs/CocoaNibs.html

Upvotes: 1

Ihar Katkavets
Ihar Katkavets

Reputation: 1570

Xib file can contain any type of serialised resources - arrays, dictionaries, not only views and view controllers. So a xib file can not have a *.h *.m files

Upvotes: 1

Wain
Wain

Reputation: 119031

An XIB file is an archive of contents and can be comprised of view controllers and views. Generally speaking, the 'built-in' view controllers aren't much use till you subclass them, so if an XIB contains a view controller then you would usually have associated .h/.m files. But the XIB could just contain views. In that case you might not have associated files (but there is a good chance you should have a view subclass as your view may want to have outlets to some of its subviews).

Upvotes: 1

Related Questions