tags:

views:

32

answers:

1

Hi there,

I'm wondering if someone from Mac development community here knows how to develop the UI for Mac which will be based on xml files. We need to re-design our software and will be working on the UI part, someone suggested to have the UI part be in xml file based so that it will be dynamic. Never tried this approach for Mac though I've know this to be familiar in Java.

The idea is to define all the control settings, buttons height, width, location etc... or the whole layout in a xml based files, have a parser that will read all of them and then form the UI.

Is there a way of doing this for Mac? Does anyone knows how?

+2  A: 

Yes it's already done. The Interface Builder which is the standard UI builder in the Cocoa world uses XIB files, which is an XML file encoding all of the UI. The XIB file and the program code are very loosely coupled, and XIB files can be independently changed without requiring re-compiling. It used to be a non-XML files called NIBs, but the difference is just implementation detail and we shouldn't care.

Objective-C and any language which has a binding to Cocoa knows how to read an XIB file and instantiate UIs frozen in it.

If you just want to have a dynamic UI decoupled from the code, it has been standard on OS X (and NeXTStep) since time immemorial. In fact, in Cocoa world, it was very rare to set up UI elements by explicitly coding them. Everybody, including Apple itself, uses Interface Builder. The world finally started to catch us up these days:)

One problem might be that the XML schema is a private thing to Apple, but there are already a few utilities available which works on XIB files. Apple's own ibtool is one example; An open-source project called nib2objc is another. This one dumps the XIB/NIB files into an equivalent Objective-C code, for ye olde timers who can't stand seeing the code to build the UI.

Addendum: I should emphasize again that the fact that XIB file is an XML file is an implementation detail from Apple's point of view, and you're usually not supposed to touch the XML file directly. What Apple wants to achieve is the loose coupling of the logic and the UI, and using XML is just a detail. So, you need to decide whether

  1. you want to use XML for interoperability, or
  2. you want to decouple UI from the code.

What you wrote in the question sounded like 2. Then Interface Builder is the perfect match. However, if 1 is what you want, the XIB file won't suit you.

Yuji
Hi Yuji, thanks for the info, so does that mean that we still need to use the interface builder when constructing for the UI? can you point me to some example wherein XML schema was used?
pinkLD
Yes, basically you need to use the Interface Builder (IB). I would say, don't re-invent the wheel; just use IB. Do you want to make a cross-platform app? Then of course the IB won't help you, you need to devise your own XML schema. I added a link to a project which directly deals with nib/xib files.
Yuji