Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Dynamically create NSButtons or NSTextFields?

Dynamically create NSButtons or NSTextFields?
Thread Tools
Fresh-Faced Recruit
Join Date: Nov 2001
Location: home
Status: Offline
Reply With Quote
Dec 9, 2004, 08:30 PM
 
Is there any way to dynamically create NSButtons or NSTextFields without having to code my own NSView that can implement the same functionality and appearance?
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Dec 9, 2004, 10:23 PM
 
NSButton inherits from NSControl, which inherits from NSView. This means you can use any of the methods in NSControl or NSView in your NSButton. Sooo, to create an NSButton programmatically, figure out the rectangle you want it to be in and do:
Code:
NSButton *myButton = [[[NSButton alloc] initWithFrame: myFrame] autorelease]; [superview addSubview: myButton]; [myButton setTitle: @"Okay"]; // etc
NSTextField is an exercise for the reader.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Samad  (op)
Fresh-Faced Recruit
Join Date: Nov 2001
Location: home
Status: Offline
Reply With Quote
Dec 10, 2004, 01:19 AM
 
I tried doing that, and I got a button with the OpenStep appearance, not Aqua. Here's the source code for the AppController:

- (void) awakeFromNib
{
NSRect frame = {{50.0, 50.0}, {100.0, 20.0}};
NSButton *myButton = [[[NSButton alloc] initWithFrame: frame] autorelease];
[[mainWindow contentView] addSubview: myButton];
[myButton setTitle: @"OK"];
}
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Dec 10, 2004, 02:04 AM
 
Take a look at the setButtonType method.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:24 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2