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 > How to make a new window?

How to make a new window?
Thread Tools
Wes
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 30, 2004, 07:51 PM
 
Simple (I think) question, I have my main window set up with a button that I would like to spawn a new (different) window. As of now my application is not set up as a multiple document app, is there an easy way to display another window? Should it be in a separate .nib file, or the same one as the main window?
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 30, 2004, 08:17 PM
 
It could be in a separate nib. It could be in the same one. It's your choice.

Keeping your MainMenu.nib as small as possible is good for your launch time, but it's not like your program is going to scream and cause a kernel panic if the two windows are in the same nib. Personally, I pretty much never have more than one window in MainMenu.nib.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
May 30, 2004, 08:36 PM
 
My general rule of thumb (taken from the documentation) for a Document-based application is that if the two windows are for the same document, then I define them in the same NIB file.

Additionally, I define general application windows all in the one NIB file (eg, Prefs, Margins, etc).
     
Wes  (op)
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 30, 2004, 08:42 PM
 
So that is settled, but how do I display the other window once the user clicks said button? (noob here, and using Java)

something like otherWindow.display()?

Make it a child of the main window?

is this even possible in a non-document based app?
     
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
May 30, 2004, 08:47 PM
 
Originally posted by Wes:
So that is settled, but how do I display the other window once the user clicks said button? (noob here, and using Java)

something like otherWindow.display()?

Make it a child of the main window?

is this even possible in a non-document based app?
Not sure about java, but in Objective-C you'd use something like:

[newWindow makeKeyAndOrderFront:self];

in the method that the button's action calls.
     
Wes  (op)
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 30, 2004, 09:01 PM
 
Ok, so in my nib file I have a window called "SomeWindow" that I want to open when the user clicks a button on "MainWindow"

in that action to to that I have to make "SomeWindow" popup. I tried...

public void makeMyWindow(Object sender) { /* IBAction */
((NSWindow)newWindow).makeKeyWindow();
}

and that didn't work. Are there any other steps I need to take?
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 30, 2004, 09:11 PM
 
Try using newWindow.makeKeyAndOrderFront(this).
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Wes  (op)
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 30, 2004, 09:17 PM
 
Thank you, that worked

I swear I'm blind sometimes...
     
Wes  (op)
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 31, 2004, 05:35 AM
 
New question now... Is it possible to make a new controller for that new window? I have tried making a new class "NewWindowController", but it gives me NullPointerExceptions whenever I try to access one of the NSTextFields in "newWindow" even though I have an outlet for the text field. Something is going on that I'm not quite grasping here...
     
Wes  (op)
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 31, 2004, 06:29 AM
 
It appears that IBAction methods can access all of the windows contents (various NSTextFields etc...) fine, but methods that are just locally implemented cannot...

     
Wes  (op)
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 31, 2004, 05:44 PM
 
for example

public void doSomething() {
((NSTextField)field).setIntValue(5);
}

doesn't work, but

public void doSomethingElse(Object sender) { /*IBAction*/
((NSTextField)field).setIntValue(5);
}

works.

In the first one I get a null pointer exception.
?
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jun 1, 2004, 05:02 AM
 
This shouldn't be the case. One reason it might appear to be, though, is if the non-IBAction method is getting called before all your objects are unpacked � until awakeFromNib() is called, there's no guarantee that any of an object's connections made in Interface Builder are valid. It's obviously impossible for an IBAction method to be called beforehand, but I don't know when the normal method is being called.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Wes  (op)
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
Jun 1, 2004, 03:52 PM
 
The non-IBAction method is called after another class calls .makeKeyAndOrderFront() on the window that is giving me trouble.

Could something be going wrong because of this?

public void toSend(Object sender) { /* IBAction */
NewController controller = new NewController()
newController.method();
}

I explicitly "create" my new controller, in my existing controller class. But like I said, all the IBAction methods work fine, and several of them are called before I call .method(). I have called awakeFromNib() and anything I do in there works fine.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jun 1, 2004, 04:00 PM
 
Originally posted by Wes:
Could something be going wrong because of this?

public void toSend(Object sender) { /* IBAction */
NewController controller = new NewController()
newController.method();
}

I explicitly "create" my new controller, in my existing controller class.
What is controller supposed to do? You are aware that it certainly won't have any connections, right?
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Wes  (op)
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
Jun 1, 2004, 04:39 PM
 
Ok, I have two windows (window, and otherWindow), and two controllers (controller, and otherController) controlling their respective windows. When a button on "window" is pressed, "otherWindow" pops up. I need to get some of the values in "window", to "otherWindow" though. I guess I'm not quite sure how to go about this. Suggestions?
     
arcticmac
Dedicated MacNNer
Join Date: Apr 2004
Status: Offline
Reply With Quote
Jun 1, 2004, 05:06 PM
 
Easy way:

put both windows in the same .nib file. drag a connection from the button to the other window's icon in the instances tab. connect on "makeKeyAndOrderFront" (or something like that) (screw using the otherWindowController)

(this way is good if you expect the user to click the button every time they launch the app)


Hard way: (better if the app doesn't necessarily need to have the window loaded in memory)

give your app's main controller class (presumably a subclass of NSObject) two outlets(otherWindow, and otherWindowController) and one action (showOtherWindow:)
in the nib with the main window, hook up the button to the action. (you have to have an instance of the main app controller) (also, create files for the main app controller if you haven't)

make a second nib with the other window. drag your header file for the main controller to it, or tell ib to read the file. use the info panel to set the main controller as the custom class for the owner instance. connect the otherWindow to the outlet for it. close IB.

define the showOtherWindow method as follows:
Code:
-(IBAction)showOtherWindow:(id)sender { if (!otherWindowController) { [NSBundle loadNibNamed:otherwindownib owner:self]; infoPanelController = [[NSWindowController alloc] initWithWindow:otherWindow]; } [otherWindowController showWindow:self]; }
the "hard way" is just a modified version of CH 16 of learning cocoa with objective-c...
     
   
 
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
Top
Privacy Policy
All times are GMT -4. The time now is 09:21 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,