开发者

XML file based UI for Mac

开发者 https://www.devze.com 2023-01-16 05:56 出处:网络
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

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 hei开发者_JS百科ght, 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?


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.

0

精彩评论

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