PDA

View Full Version : building dynamic interfaces in Xcode apps




ZacharyDenison
Feb 27, 2008, 08:53 AM
Hello,

I am trying to build an application in xcode that would allow me to request information from the user in a type of form (similar to an HTML form). The problem is that I don't know the fields until runtime. How can I generate the GUI (text boxes, buttons, radio buttons etc..) from dynamic information at run time. For example -- at run time i will parse a file that contains "Name, Address, Zip Code ... etc" and from that I want to generate the gui and have people enter the information -- ?

Thank you.

Zach.



kainjow
Feb 27, 2008, 09:02 AM
Xcode is just an IDE. It can be used with Java, C, C++, Objective-C, AppleScript, etc. You need to be more specific on what language/API you're using.

ZacharyDenison
Feb 27, 2008, 11:13 AM
I am using Objective-C.

kainjow
Feb 27, 2008, 12:06 PM
Well you'll need to familiarize yourself with controls and creating them on the fly. Read the docs on NSView, NSTextField, and NSButton.

Parsing the file will vary depending on the file format. Easiest way is to use a plist and read it in as an NSDictionary/NSArray. But you could also use XML.

ZacharyDenison
Feb 27, 2008, 12:13 PM
that was basically my question, how to create those elements on the fly.

robbieduncan
Feb 27, 2008, 01:03 PM
This code would create an NSTextField and add it to the content view of a window:


-(void) addTextFieldToWindow:(NSWindow *) window inFrame:(NSFrame) frame
{
NSTextField *field = [[NSTextField alloc] initWithFrame:frame];
[field autorelease]; // This assumes you are not using garbage collection
[[window contentView] addSubview:field];
}


Note that this does not ensure that the added view is visible! I would imagine that you would actually want to specify the view you are adding it to. I would have thought you would be using a NSScrollview to contain your content so you would actually add the view to the document view of the scroll view...