我需要重新定位一个NSAlert窗口。我已经发现,要做到这一点,我需要在委托中实现(NSRect)window:(NSWindow *)window willPositionSheet:(NSWindow *)sheet usingRect:(NSRect)rect
方法。为什么我的窗口:willPositionSheet:usingRect:委托方法未被调用?
我的代码,我触发警报窗口是在下面的方法:
- (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
NSAlert* jsAlert = [[NSAlert alloc] init];
[jsAlert addButtonWithTitle:@"OK"];
[jsAlert setMessageText:@""];
[jsAlert setInformativeText:message];
[jsAlert setAlertStyle:NSWarningAlertStyle];
// [jsAlert setDelegate:self];
[jsAlert beginSheetModalForWindow:sender.window modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:NULL];
}
在那里,我设置modalDelegate
所以self
,其中self
是AppDelegate
。我在AppDelegate
中实现了以下方法:
- (NSRect)window:(NSWindow *)window willPositionSheet:(NSWindow *)sheet usingRect:(NSRect)rect {
NSLog(@"we are here: %s", __func__);
return NSMakeRect(0, 0, 100, 100); // just fake
}
此方法不会被调用。从我在文档中发现的这个应该可以工作,但我无法理解它为什么不能。
我还尝试了以下操作,即使AppDelegate符合NSWindowDelegate
协议(和NSAlertDelegate
协议,在注释第一个代码段中的未注释行时)。
@interface AppDelegate : NSObject <NSApplicationDelegate, NSAlertDelegate, NSWindowDelegate>
我到处看到这应该只是工作。我在这里做错了什么?
不用说,我是在Cocoa开发一个新手;) – beipawel