如何正确设置NSWindow代表?

时间:2016-01-01 11:12:02

标签: objective-c cocoa

我正在尝试以程序方式实例化新窗口,然后配置其委托对象。

不幸的是,委托对象在设置后似乎没有收到任何事件。

我试图将我的代码简化为一个简短的例子:

AppDelegate.h

@interface AppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate>
- (IBAction) clicked:(id)sender;
@end

AppDelegate.m

@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate {
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Main app window delegate works properly
    [[self window] setDelegate:self];
}

// Button clicked: instantiate a new window from a specific nib (default)
- (IBAction) clicked:(id)sender {
    static NSWindowController* foo;

    foo = [[NSWindowController alloc] initWithWindowNibName: @"ExampleWindow"];
    [[foo window] setDelegate:self];
    [foo showWindow:self];
}


// Will correctly trigger for main window, but not for newly created windows
- (void) windowWillMove:(NSNotification *)notification {
    NSWindow* moved = (NSWindow*)[notification object];
    NSLog(@"Window %p (%@) will move", moved, [moved title]);
}

@end

我可能错过了一些明显的东西,但我无法确定是什么

0 个答案:

没有答案