2014-07-24 249 views
0

我需要重新定位一个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,其中selfAppDelegate。我在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> 

我到处看到这应该只是工作。我在这里做错了什么?

+0

不用说,我是在Cocoa开发一个新手;) – beipawel

回答

2

唯一剩下要做的就是实际注册AppDelegateNSWindowDelegate这样的:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    // [...] 
    [self.window setDelegate:self]; 
    // [...] 
} 

这听起来很琐碎,但我需要一个地狱很多时间去发现出来,因为所有的例子我看到没有这样做。他们只是将modalDelegate设置为self,在我的情况下应该是AppDelegate

我希望这有助于有人出来......

+0

是的,这是令人困惑的。模态委托只是接收did-end选择器的对象。这是它唯一的作用。它不是窗口的代表,也不是表格的代表,而是表格演示操作的代表。这是窗口代表的完全独立的概念。顺便说一句,' - [NSAlert beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:]'已被弃用。您应该使用'-beginSheetModalForWindow:completionHandler:',而不是目标10.9或更高版本进行部署。 –

+0

thx的评论。我知道该方法已被弃用,但我也需要以10.8为目标,所以我认为我会使用旧的方法。我想有一种方法来检查版本并使用旧版或新版,但这是另一次:-) – beipawel

相关问题