2
votes

I have a NSWindow that I am launching makeKeyAndOrderFront to the window from a MenuBarItem in an .xib file - it works, great.

However, I want to run a method when this window opens, so I subclassed NSWindow and set the window to the subclass.

When I do the shortcut, the window opens but is not the main window, when doing -(void)makeKeyWindow

When I do -(void)makeMainWindow, the NSLog does nothing.

And, when I override makeKeyAndOrderFront, it doesn't show at all (as expected).

Any help appreciated! Thanks!

1
What controller owns this window? With an NSWindowController subclass, you could use windowDidLoadstevesliva
@stevesliva, the window loading is not the same thing as the window showing.Ken Thomases
The method is -makeKeyAndOrderFront: with a colon, not just -makeKeyAndOrderFront. That's an important distinction. You may have failed to override the existing method and just created a new method that nobody ever calls. By the way, since you're calling -makeKeyAndOrderFront: to show the window, why do you need to be called to know when it's shown? The code which calls -makeKeyAndOrderFront: already knows.Ken Thomases
@KenThomases Sorry, yes, I am using it with a colon. I call makeKeyAndOrderFront from Interface Builder. However, I want to run a method when that window is shown, so when I implement that method in my NSWindow subclass, I get an NSLog(), but the window itself is not shown...1789040
I guess you're not calling through to super.Ken Thomases

1 Answers

3
votes

The reason that the window stopped showing up when you overrode -makeKeyAndOrderFront: is that you neglected to make your override call through to super's implementation.

[Edit by Asker]

To add code, here's what it should be:

- (void)makeKeyAndOrderFront:(id)sender {
    // what to do when the window is shown:

    [super makeKeyAndOrderFront:sender]; // key line, you MUST have this
}