开发者

How are *.xib, *.m and *.h files related?

开发者 https://www.devze.com 2022-12-23 05:46 出处:网络
How/where in the code does the nib file get related to its *.m and *.h files? Specifically I\'m talking about when you create a new Objective-C class, not using the initWithNibName method.

How/where in the code does the nib file get related to its *.m and *.h files?

Specifically I'm talking about when you create a new Objective-C class, not using the initWithNibName method.

Where开发者_Go百科ver this relationship is defined is obviously used by the 'Files Owner' relationship in Interface Builder. I imagine it's a *.plist file somewhere or something.


NIB files describe classes instances.

When you drag and drop a component in a NIB with Interface Builder, actually, you create an instance.

If you drag and drop a UIViewController for instance, and if you did define a subclass of UIViewController in your OWN classes with a .m and a .h, you'll be able to say to interface builder : "use my custom subclass of UIViewController for this object instance".

This is achieved in interface builder with Tools/Identity Inspector and then Class identity : at this place, you can put your own classes that indirectly refer to your own .h and .m


NIB files describe interface. They're produced from XIB files. XIB files are text (XML, to be precise), NIBs are binary. You typically edit XIB files in Interface Builder, and they get compiled into NIBs as the project is built.

.h files contain class interfaces. Interface Builder uses those to compile a list of Objective C classes in your app, and to produce the list of outlets and actions they provide. When you design a XIB, you typically provide one of your ViewController classes as the file owner. Interface Builder would scan a .h file and produce the outlet/action list from it.

.m files contain code. They're not used by the Interface Builder directly. But they can and often do contain code that loads those NIB files. And, of course, they contain code of action handlers.


Although the question is marked as answered, I don't really see the answer that was asked for...

Usually the code itself specifies the relation when creating a view controller, like so:

MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];

That tells the system to create the view controller instance, and look for "MyView.xib" to load and wire to MyViewController - MyViewController would also have been set to file's owner in the XIB.

Now another way to relate your view controller and the xib, is that in some cases you are actually creating view controller instances in a xib. The tab bar controller is a great example, where for a tab you specify a view controller instance to create, along with the nib name that it will use.

Note this means you can have several xib files that make use of a single view controller, if you want the same controller to with with different kinds of screens. You don't end up doing that often but it can be powerful, when used correctly (it can also be a mess if you try to make one view controller that does too many things).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号