2

I want to find a good resource and/or book on this matter. I am already really comfortable and efficient with creating apps with interface builder and see the value in using it in terms of all the visual feedback it provides in realtime / how good of a time saver it is.

So many questions like this were answered by people trying to persuade people to be ignorant of how to create an interface programmatically and to just use interface builder all the time. I see the value in using IB but I feel that my knowledge of Cocoa is incomplete without knowing how to create an interface from both approaches.

Any help is gladly appreciated! Thanks in advance!

Filip Radelic
  • 26,607
  • 8
  • 71
  • 97
Nirma
  • 5,640
  • 4
  • 35
  • 47
  • This link seems to answer your question: http://stackoverflow.com/questions/717442/how-do-i-create-cocoa-interfaces-without-interface-builder –  Aug 09 '11 at 17:08
  • possible duplicate of [Loading a view controller & view hierarchy programatically in Cocoa Touch without xib](http://stackoverflow.com/questions/809898/loading-a-view-controller-view-hierarchy-programatically-in-cocoa-touch-without) – Brad Larson Aug 09 '11 at 18:01
  • See also [iPhone app without using Interface Builder](http://stackoverflow.com/questions/2472691/iphone-app-without-using-interface-builder) – Brad Larson Aug 09 '11 at 18:02

1 Answers1

2

Every setting in the Interface Builder has a corresponding property in the object. For example, compare the Interface Builder inspector for NSTextField/UITextField and the documentation of NSTextField/UITextField.

IBOutlet and the target/action mechanism are not special either: the former just uses Key-Valued Coding to set the outlet to an object, and the latter just uses setTarget: and setAction: of NSControl (or a similar method of UIControl on iOS.)

What the IB does is to precook these objects with the properties set as you specify in the inspector. When loaded, the data is fed to initWithCoder: of the class.

So, if you'd like to go IB-less just for fun, all you have to do is to alloc+init the UI object, and set all the properties by code. There's nothing more than that (except for special menu items on OS X which is somehow handled implicitly when MainMenu.nib is loaded, that is.)

There is an open source project called nib2objc which translates a nib/xib to a .m file with explicit Cocoa calls. That will help you understand what is going on.

On a bit more about nib files, read Apple's own documentation. The special magic at the loading of MainMenu.nib is described in this series of blog posts. But this is not really about how you would create UI without the nib in general; it's about how the special menu items are treated by the system.

In any case, internally speaking, there's almost no difference between loading a nib and writing UI codes programatically; both just create UI objects and set the UI properties appropriately. With IB, you set the properties beforehand and the precooked objects are read at the run time. If you do it in your code, the properties are set at run time. But once they are set, the resulting objects are exactly the same. So, there's really not much to learn.

Yuji
  • 34,103
  • 3
  • 70
  • 88
  • Thank you very much for this answer, It is good but Im looking for a book / tutorial on this. Do you know any? – Nirma Aug 09 '11 at 17:23
  • 1
    There's not much to be learned (except for the magic which happens at the loading of `MainMenu.nib`.) I'll add a few links ... see the edited answer. – Yuji Aug 09 '11 at 17:48