Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSNotification addObserver选择器无法在窗口上打开NSBeginAlertSheet工作表_Objective C_Cocoa_Nsnotificationcenter_Alerts - Fatal编程技术网

Objective c NSNotification addObserver选择器无法在窗口上打开NSBeginAlertSheet工作表

Objective c NSNotification addObserver选择器无法在窗口上打开NSBeginAlertSheet工作表,objective-c,cocoa,nsnotificationcenter,alerts,Objective C,Cocoa,Nsnotificationcenter,Alerts,我有两个不同的窗口控制器。第一个是自定义面板窗口控制器,另一个是主窗口控制器。在面板窗口中有一个面板窗口,面板上有一个按钮。单击这些按钮后,我将发布通知,如: In PanelWindowController: -(IBAction)okAndCancelButtonClicked:(id)sender { [self postNotification:sender]; } -(void)postNotification:(id)sender

我有两个不同的窗口控制器。第一个是自定义面板窗口控制器,另一个是主窗口控制器。在面板窗口中有一个面板窗口,面板上有一个按钮。单击这些按钮后,我将发布通知,如:

In PanelWindowController:
    -(IBAction)okAndCancelButtonClicked:(id)sender
    {
        [self postNotification:sender];
    }

    -(void)postNotification:(id)sender
    {
        if([sender tag]!=2){
            [[self window]  endSheet:self.panel returnCode:NSModalResponseCancel];
            [self.panel orderOut:self];
        }
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:[sender tag]],@"value",nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PanelButtonClickedNotification" object:self userInfo:dict];
    }
现在,在我的主窗口控制器中,我正试图在
NSNotificationCenter
addObserver
选择器中打开一个
NSBeginAlertSheet
。以下是我的主窗口控制器的
init
方法中的
addObserver
selector
声明:

MainWindowController

-(id) init{
..// some code here
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(okButtonClicked:) name:@"PanelButtonClickedNotification" object:[self panelClass]];
return self;
}
okButtonClicked
的实现如下:

- (void) okButtonClicked:(NSNotification*)notification
{
    if ([[notification object] isEqualTo:[self panelClass]])
    {
        if([[[notification userInfo] objectForKey:@"value"] integerValue] == 1)
        {
            // Yes Button in the Panel is clicked
        }
        else if([[[notification userInfo] objectForKey:@"value"] integerValue] == 0)
        {
            // No Button in the Panel is clicked
            NSBeginAlertSheet(@"Alert", @"Ok", nil, nil, [[self view] window], self,nil, nil,nil,@"Alert is being shown on window.");
        }

    }
}
当用户单击面板上的“无”按钮时,应在窗口上显示警报。但警报从未显示。我还尝试了
[NSApp keyWindow]
[NSApp mainWindow]
而不是
[[self view]window]
。 并且,如果我独立于窗口运行警报,它将显示:

NSAlert *alert = [[NSAlert alloc] init];
 [alert setMessageText:@"Alert"];
 [alert addButtonWithTitle:@"OK"];
 NSImage *icon=[NSImage imageNamed:@"warning.png"];
 [alert setIcon:icon];
 [alert runModal];
如果我遗漏了什么,请告诉我

在收到通知后调用的任何方法中都不会显示此警报。
PFA我的示例项目:

有点难判断,但我最好的猜测是,在okButtonClicked:函数中,对通知作出反应的对象没有对窗口的(有效)引用。然后,警报不知道在哪个窗口显示

如果您可以在通知对象中发送对窗口的引用,那么您应该能够获得所需窗口上显示的警报

示例代码:

[[NSNotificationCenter defaultCenter] postNotificationName:@"PanelButtonClickedNotification" object:[self window]];
以及:


这在我的测试项目中起作用

问题不在于通知,而是面板造成了问题: 我正在打开主窗口控制器中的面板:

MainWindowController

-(id) init{
..// some code here
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(okButtonClicked:) name:@"PanelButtonClickedNotification" object:[self panelClass]];
return self;
}
[[self window]beginSheet:self.panelClass.panel completionHandler:nil]

面板的关闭动作写在面板窗口控制器中。因此,在主窗口上加载/卸载自定义面板后,不会显示任何
NSBeginAlertSheet

因此,将以下代码从面板窗口控制器移动到主窗口控制器解决了问题:

[[self window]尾页:self.panelClass.panel returnCode:NSModalResponseCancel];

[self.panelClass.panel orderOut:self]

我在发布通知时发送了一些
userInfo
。NSDictionary*dict=[NSDictionary Dictionary WithObjectsAndKeys:[NSNumber numberWithInteger:[sender tag]],@“value”,nil];[[NSNotificationCenter defaultCenter]postNotificationName:@“PanelButtonClickedNotification”对象:self userInfo:dict];因此,我将无法将
窗口
作为通知对象发送。您可以将其添加到正在创建和解析的dict中。当然,只有当它存在/类知道窗口时。NSDictionary*dict=[NSDictionary Dictionary WithObjectsSandKeys:[NSNumber numberWithInteger:[sender tag]],@“值”,[self window],@“window”,nil];然后检索它[[notification userInfo]objectForKey:@“window”]在发布通知时无法通过
[self window]
,因为它位于单独的窗口控制器中(在该控制器中,只有面板窗口上有按钮,按下这些按钮时,通知被发送)。主窗口在另一个窗口控制器中,我在其中接收通知。。。此外,一旦面板在主窗口上打开并发送/接收通知,我将无法使用任何其他方法打开NSBeginAlertSheet:(