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 placing gui

Dynamically placing gui
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
Feb 3, 2004, 03:33 PM
 
This may be a simple question, but how do you place items dynamically in a GUI? I need to find X amount of items, which can be changed, and place them in a window. I can set it up with a static amount no problem, but dont have a clue how to do it dynamically. I am trying to use Cocoa w/out Java. I can do it in Java via swing no problem. I need to do this in Objective-C.

Documentation link to developer.apple.com should be fine, unless you want to put code. Thanks!
     
Fresh-Faced Recruit
Join Date: Feb 2004
Status: Offline
Reply With Quote
Feb 3, 2004, 05:23 PM
 
NSWindow *window...
NSView *contentView = [window contentView];

NSButton *btn = [[NSButton alloc] initWithFrame: NSMakeRect(10, 10, 120, 30)];
[btn setTitle: @"Hello World!"];
[btn setBezelStyle: NSRoundedBezelStyle];

[contentView addSubview: btn];
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
Feb 3, 2004, 06:14 PM
 
Thanks! How about a new NSTextField and possibly a new NSImageView? I will look in the documentation tonight and see if I can do the same thing.
     
Fresh-Faced Recruit
Join Date: Feb 2004
Status: Offline
Reply With Quote
Feb 3, 2004, 06:51 PM
 
This works in the exact same manner for all subclasses of NSView.
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
Feb 3, 2004, 07:07 PM
 
Yes I see...

NSWindow *window...
NSTextView *textView = [window textView];

NSTextField *text = [[NSTextField alloc] initWithFrame: NSMakeRect(10, 10, 120, 30)];

[textView addSubview: text];

Does this sound like it would work? I will test it our in the morning when I am on my mac.
     
Fresh-Faced Recruit
Join Date: Feb 2004
Status: Offline
Reply With Quote
Feb 3, 2004, 07:26 PM
 
Nope

NSWindow *window...
NSView *contentView = [window contentView];
NSTextField *text = [[NSTextField alloc] initWithFrame: NSMakeRect(10, 10, 120, 30)];
[contentView addSubview: text];


The contentView method is equivalent to JFrames getContentPane() method. You can either add or remove subviews from this root view or you can set your own root view. E.g.

NSTextField *text = [[NSTextField alloc] initWithFrame: NSMakeRect(10, 10, 120, 30)];
[window setContentView: text];

The result is a window with a NSTextField which has the size of the window.
     
Professional Poster
Join Date: Sep 2000
Location: Texas
Status: Offline
Reply With Quote
Feb 3, 2004, 07:32 PM
 
Ah ha! I will try that tomorrow. I think it is exactly what I was needing!!! Thanks!

EDIT: How do you get the current window? ie:

NSWindow *window = [self window]; ???

Sorry for these easy questions, my next/better Cocoa book is still being ordered...
(Last edited by djohnson; Feb 3, 2004 at 07:45 PM. )
     
Fresh-Faced Recruit
Join Date: Feb 2004
Status: Offline
Reply With Quote
Feb 4, 2004, 04:51 AM
 
You can create it yourself...

NSWindow *window = [[NSWindow alloc]
initWithContentRect: NSMakeRect(10, 10, 100, 100)
styleMask: NSTitledWindowMask | NSClosableWindowMask
backing: NSBackingStoreBuffered
defer: NO];

...or you get it from IB as an outline or [NSApp keyWindow] which returs the NSWindow that receives keyboard events (i.e. the "current" window)...
     
   
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 12:56 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