0

I have an Objective C application which starts with loading a window created with Interface Builder:

//in main()
[NSApplication sharedApplication];
[NSBundle loadNibNamed:@"MainMenu" owner:NSApp];
[NSApp run];

In MainMenu.xib I have a window with a button. I want to create programmatically the second window when that button is pressed.

  //in MainMenu.xib Controller.h
  @class SecondWindowController;

  @interface Controller: NSWindowController {
     @private
     SecondWindowController *sw;
  }
  - (IBAction)onButtonPress:(id)object;
  @end

//in MainMenu.xib Controller.m
#import "SecondWindowController.h"

@implementation Controller
- (IBAction)onButtonPress:(id)object {
  sw = [[SecondWindowController alloc] initWithWindowNibName:@"SecondWindow"];
  [sw showWindow:self];
  [[self window] orderOut:self];
}
@end

Where SecondWindowController inherits from NSWindowController. In SecondWindowController.h I have:

- (id)initWithWindow:(NSWindow*)window {
  self = [super initWithWindow:window];
  if (self) {
    NSRect window_rect = { {custom_height1, custom_width1}, 
                           {custom_height2,    custom_width2} };
    NSWindow* secondWindow = [[NSWindow alloc] 
                         initWithContentRect:window_rect
                         styleMask: ...
                         backing: NSBackingStoreBuffered
                         defer:NO];                         
  }
  return self;
}

And in the SecondWindow.xib I have nothing. When the button of the first window is pressed the first window disappears and the application closes. The reason I don't want to use the Interface builder for the second window is that I want to programmatically initialize it. Is this possible and if so what is the right way to accomplish this?

4
  • That second window controller needs to be an instance variable so it's doesn't go out of scope in onButtonPress:.
    – trojanfoe
    Oct 28, 2013 at 15:18
  • I'm sorry, it is, I forgot to mention it... Oct 28, 2013 at 15:21
  • I have no window object in SecondWindow.xib, I thought I should create the window in the code. Should I add one? Oct 28, 2013 at 15:31
  • No, see my answer. I was confused by your contradictory code and requirements...
    – trojanfoe
    Oct 28, 2013 at 15:33

1 Answer 1

0

OK, I was initially confused with your use of initWithWindowNibName:@"SecondWindow" which will attempt to load the window from a NIB file, which you later mention you don't want to do.

Please use this to create your window:

- (IBAction)onButtonPress:(id)object {
    if (!sw)
        sw = [[SecondWindowController alloc] init];
    [sw showWindow:self];
    [[self window] orderOut:self];
}

Which will avoid creating multiple copies of the window controller, which you don't want (if you do then you'll need to store them in an array). Note the name sw is incorrect by convention; use either _sw or create setter/getter methods and use self.sw.

Initialize SecondWindowController like this:

- (id)init {
    NSRect window_rect = NSMakeRect(custom_x, custom_y,
                                    custom_width, custom_height);
    NSWindow* secondWindow = [[NSWindow alloc] 
                         initWithContentRect:window_rect
                         styleMask: ...
                         backing: NSBackingStoreBuffered
                         defer:NO];                         

    self = [super initWithWindow:secondWindow];
    if (self) {
         // other stuff
    }
    return self;
}

Note: your variable names for origin/size of the new window were wrong; please review them.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.