Programmatically Add UI Controls in Objective-C

varBoomer
varBoomer used Ask the Experts™
on
I'm pretty new to Objective-C, but I do understand a lot of the syntax and the way it handles objects through messages.  But that's about it.  

One thing I'm trying to learn is how to add UI controls programmatically and I found some sample code to try and do this.  However, I'm not sure what method I need to "paste" the following code into.  It's for an NSButton.  

This is the sample code I found:
NSView *superview = [window contentView]; 
NSRect frame = NSMakeRect(10, 10, 200, 100); 
NSButton *button = [[NSButton alloc] initWithFrame:frame]; 
[button setTitle:@"Click me!"]; 
[superview addSubview:button]; 
[button release];

Open in new window


Any help is appreciated!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Wow, no response in quite some time... Either there is another forum somewhere that is specific to apple or my question just makes absolutely no sense at all...  Whatever the reason is for no response, please let me know!
Commented:
The code you posted creates a button and add it to the view. The button title is "Click Me!". No action will be performed when you'll click on this button.
That's the code creating the button:

NSRect frame = NSMakeRect(10, 10, 200, 100); 
NSButton *button = [[NSButton alloc] initWithFrame:frame]; 
[button setTitle:@"Click me!"]; 

Open in new window


This code adds the button to the parent view
[superview addSubview:button];

Open in new window


It's code for Mac OS X, not for iOS.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial