Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I thought this new project would be a no brainier. Have my main NSWindow have an open button and when pressed a second NSWindow appears with a button on it that says close.

Are there any tutorials for opening a second window? I thought this would be fun for my Friday night and I can't figure it out? :(

-Lars
 
I don't know of any tutorials. A couple of approaches come to mind.

The first approach, which would also be the most straightforward of the two, would be to have two windows in MainMenu.xib. Only the main window would have Visible At Launch set, the other would have Visible At Launch unset. The app delegate would have outlets for both windows. The action connected to the open button in the main window would send makeKeyAndOrderFront: to the second window. The action connected to the close button in the second window would send orderOut: to the second window.

The second approach, which would be more difficult but more correct, would be to have the second window it's own .xib file and load the second .xib on demand. But I would attempt the first approach before attempting the second.
 
I have the AppDelegate.h where the first window was create automatically. I then dragged in a second window and control dragged it to the AppDelegate.h
Code:
@interface addedWindowAppDelegate : NSObject <NSApplicationDelegate> {
@private
    NSWindow *window;
    NSView *secondWindow;
}

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSView *secondWindow;

@end
I then created a new class called AppController.h which I then control dragged my open button too to make an IBAction for the button.
Code:
#import <Foundation/Foundation.h>
#import "addedWindowAppDelegate.h"


@interface AppController : NSObject {
@private
    
}
- (IBAction)openSecondWindow:(id)sender;

@end

But in the Method for the button when I try to even type [secondWindow ...] I get an error "Use of undeclared identifier 'secondWindow'"

I did a #import "addedWindowAppDelegate.h" so I would think it would see it? Should my Open Window button IBAction be located in the AppDelegate.h or is it fine in the AppController.h ?

-Lars
 
For a start, you've declared secondWindow as an NSView, not an NSWindow.
 
I did see that. But I dragged a new window object in to the interface builder. Then I control dragged into the appDelegate.h and create and it creates a new NSView, not an NSWindow? When I look at the object in the object list is says it is an NSWindow and I end up with an NSView some how?

-Lars
 
This is simple test app so do everything in the app delegate, including the openSecondWindow: and closeSecondWindow: IBAction methods.
 
That might be what I was doing wrong, I will reset again tonight and start from scratch.

Thanks Jim

-Lars
 
I can't believe I am struggling with this. Every where online everyone says it is simple. so I must be doing something wrong. I started with the default window in the Main Menu.xib. Then from the objects I drag out the 'window' object so I have 2 windows in the Main Menu.xib. But when I control drag from my second window to the appDelegate it automatically calls it an NSView which I label it windowTwo.
Code:
@interface testSecondWindowAppDelegate : NSObject <NSApplicationDelegate> {
@private
    NSWindow *window;
    IBOutlet NSView *windowTwo;
}

@property (assign) IBOutlet NSWindow *window;

- (IBAction)openSecondWindowButton:(id)sender;

@end

Now in the implementation I write this

Code:
@implementation testSecondWindowAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    
}

- (IBAction)openSecondWindowButton:(id)sender {
    [windowTwo makeKeyAndOrderFront: self];
}
     @end
I am getting an error for the NSView and I understand it because the code'makeKeyAndOrderFront:' And that is because it is not a method of the NSView but a method of the NSWindow. This is really making me angry because it seems so stupidly simple. Why can I not drag a WINDOW OBJECT that says it so an NSWindow to the Main Menu.xib, and when connected to the appDelegate it converts to an NSView and no staying an NSWindow ?

ahhhhh
 
You might be connecting it to the content view. Try connecting to the window title, or the window object that shows inside the nib (next to First Responder, etc).
 
Yep, I just tried it out by control dragging the bar up top above to the App Delegate and that was the problem. When I click dragged the window I got an NSView but when I selected the bar up top I got an NSWindow.

Such a small thing that caused so much wasted time. No where did I read you needed to control drag from the window bar and not within the window it's self.

Thanks for that information.

-Lars
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.