|
|
How to make a new window?
|
|
|
|
Junior Member
Join Date: Apr 2001
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
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'."
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
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).
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Apr 2001
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Apr 2001
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Try using newWindow.makeKeyAndOrderFront(this).
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Junior Member
Join Date: Apr 2001
Status:
Offline
|
|
Thank you, that worked
I swear I'm blind sometimes...
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Apr 2001
Status:
Offline
|
|
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...
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Apr 2001
Status:
Offline
|
|
It appears that IBAction methods can access all of the windows contents (various NSTextFields etc...) fine, but methods that are just locally implemented cannot...
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Apr 2001
Status:
Offline
|
|
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.
?
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
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'."
|
|
|
|
|
|
|
|
Junior Member
Join Date: Apr 2001
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
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'."
|
|
|
|
|
|
|
|
Junior Member
Join Date: Apr 2001
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Apr 2004
Status:
Offline
|
|
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 Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|