40

I would prefer to create my interfaces programatically. Seems as if all the docs on Apple Developer assume you're using Interface Builder. Is it possible to create these interfaces programatically, and if so where do I start learning about how to do this

I thought the relevant document for this, if possible would be in this section: http://developer.apple.com/referencelibrary/Cocoa/idxUserExperience-date.html

1
  • 9
    "I would prefer to create my interfaces programatically" +1 for this, you rock.
    – user529758
    Oct 28, 2012 at 15:38

5 Answers 5

38

I like the question, and I'd also like to know of resources for going IB-less. Usefulness (the "why") is limited only by imagination. Off the top of my head, here are some possible reasons to program UIs explicitly:

  • Implementing a better Interface Builder.
  • Programming dynamic UIs, i.e., ones whose structure is not knowable statically (at compile/xcode time).
  • Implementing the Cocoa back-end of a cross-platform library or language for UIs.

There is a series of blog posts on working without a nib and a recent description by Michael Mucha on cocoa-dev.

3
  • 10
    I like the question too. In fact I wonder how the accepted answer is basically that your an idiot if you want to do it this way. I just went xib-less for an iOS app (this tutorial helped a lot: vimeo.com/3363949 ) and am now looking to do the same on the desktop.
    – Lou Zell
    Dec 14, 2010 at 8:56
  • Here's an updated link to the blog posts since the link above just takes you to the main page: lapcatsoftware.com/blog/2007/05/16/working-without-a-nib-part-1
    – MattB
    Mar 7, 2013 at 15:03
  • Thanks, @MattB. I've updated my answer to include the direct URL you found.
    – Conal
    Mar 7, 2013 at 22:24
20

I would prefer to create my interfaces programatically.

Why? Interface Builder is easier and faster. You can't write a typo by drag and drop, and you don't get those oh-so-handy Aqua guides when you're typing rectangles by hand.

Don't fight it. Interface Builder is your friend. Let it help you.

If you insist on wasting your own time and energy by writing your UI in code:

Not document-based (generally library-based, like Mail, iTunes, iPhoto): Create a subclass of NSObject, instantiate it, and make it the application's delegate, and in the delegate's applicationDidFinishLaunching: method, create a window, populate it with views, and order it front.

Document-based (like TextEdit, Preview, QuickTime Player): In the makeWindowControllers method in your subclass of NSDocument, create your windows (and populate them with views) and create window controllers for them, making sure to send yourself addWindowController: for each window controller.

18
  • 12
    Amen. I used to be of that "write everything myself" mindset. Then I learned what it meant to meet project deadlines. It's a real eye-opener.
    – Rob
    Apr 4, 2009 at 18:06
  • 2
    I triple that. Interestingly, I've had a few developers coming from the Java world recently also say they prefer to code the interface themselves... right up until they understood that IB doesn't generate any code and doesn't need to. Guess the IDE-generated swing code really sucks :-) Apr 4, 2009 at 19:51
  • 27
    "You can't write a typo by drag and drop" actually, I've discovered, you can.. I've caused weird application-crashing errors by accidently dragging to the wrong outlet or binding. Interface Builder is okay, but it's not foolproof (not to say creating interfaces by hand is any better, but if nothing else the errors in ObjC are a bit more.. obvious)
    – dbr
    May 4, 2009 at 1:32
  • 6
    btw, Apple avoids using IB too: lapcatsoftware.com/blog/2008/10/20/…
    – yairchu
    Jun 2, 2010 at 14:58
  • 2
    The #1 reason not to use IB is not all the world is Cocoa. If your target includes Windows or Unix, any work you do with Mac-only tools has to be done again anyway. The #2 reason is that not all projects are new. The minimal step to continue using existing code shoudln't start with replacing the entire UI.
    – ddyer
    Jan 31, 2012 at 18:59
19

As a completely blind developer I can say that IB is not compatible with VoiceOver (the built-in screen-reader on OS X).

This means that without access to robust documentation on using Cocoa without IB I cannot develop apps for OS X / iPhone in Cocoa, which means I (ironically) cannot easily develop apps that are accessible to the blind (and all others) on OS X / iOS.

My current solution, which I would prefer not to use, is Java + SWT, of course this works for OS X, not so much for iOS.

2
  • I think the documentation is there. It's just not in the howto's which sucks. :/
    – Bjorn
    Sep 25, 2010 at 0:08
  • For iOS there's been some success using teacup to build UIs (Austin Seraphin at least has used it successfully). github.com/rubymotion/teacup
    – colinta
    May 2, 2013 at 23:44
4

In fact IB becomes totally unusefull when you start to write your own UI classes. Let say that you create your own button that use an skin system based on a plist. Or you create an dinamic toolbar that load and unload items based on user selection.

IB doesn't accept custom UI elements, so more complex UI can't use him. And YES you will want to do more complex things that the UIKit gives you.

4
  • 4
    You can definitely create your own IB elements. developer.apple.com/mac/library/documentation/DeveloperTools/…
    – kubi
    Jun 18, 2010 at 21:58
  • Light at end of the tunnel!! Works for IB on iPhone too?
    – Paulo
    Aug 9, 2010 at 0:16
  • Nice post about this subject: cocoawithlove.com/2009/07/… . And i'm not sure yet, but this technique doesn't work on iPhone yet. :(
    – Paulo
    Aug 9, 2010 at 0:28
  • You can use IB_DESIGNABLE and IBInspectable now as of Xcode 6. IB_DESIGNABLE means that your custom view will render in IB and IBInspectable on a property means that you can set the property's value in IB.
    – Joel
    Jan 10, 2015 at 3:47
3

Though this is quiet a bit old... I tried many times to do everything only with programmatically. This is hard, but possible.

Update: I posted another question for this specific issue: View-based NSOutlineView without NIB?, and now I believe everything can be done in programmatical way, but it's incredibly hard without consulting from Apple engineers due to lack of information or examples.


Below argument might be off-topic, but I like to note why I strongly prefer programmatically way.

I also prefer programmatic way. Because

  • Static layout tool cannot handle anything dynamic.
  • Reproducing same UI state across multiple NIBs is hard. Everything is implicit or hidden. You need to visit all the panels to find parameters. This kind of job is very easy to make mistake - mistake friendly.
  • Managing consistent state is hard. Because reproducing same look is hard.
  • Automation impossible. You cannot make auto-generated input form.
  • Parameter indirection - such as variable element size chosen by user - is not possible.
  • Aiming small point is a lot harder than hitting finger sized keys at fixed location - funny that this is serious usability issue for developers!
  • IB sometimes screws. Which means it's compilable, and still working, but when I open the source, it looks broken and extra editing becomes impossible. (you may not experienced this yet, but if XIB file goes complex, this must happen)
  • It's image based serialization. The concept is good. But the problem is image-base only. IB doesn't keep the source code for clean boot by replaying the source code. Clean boot is very important to guarantee specific running state. Also, we cannot fix the bugs in source-code. Bug s just will be stacked infinitely. This is core reason why we cannot reproduce the equal(not similar looking) UI state in IB.

Of course these stuffs can be solved by post-processing NIB UI, but if we have to configure everything again, there's no reason to use IB at first.

With text code, it's easy to reproducing the same state - just copy the code. Also easy to inspecting and fixing wrong part - because we have full control. But in IB, we have no control on hard-core details.

IB can't be ultimate solution. It's like a Photoshop, but even Photoshop offers text-based scripting facility. GUI is a moving program, and not a static image or graphic. An IB approach is completely wrong even for visual editing of GUI. If you're one of the Apple folks reading this, I beg you to remove whole dependency to IB completely ASAP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.