2015-02-24 48 views
0

我有兩個不同的窗口控制器。首先是一個自定義面板窗口控制器,另一個是主窗口控制器。面板窗口中有一個面板窗口,該面板上有按鈕。在這些我張貼通知如按鈕的點擊:NSNotification addObserver選擇器無法在窗口上打開NSBeginAlertSheet表格

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]; 
    } 

現在,在我的主窗口控制,我想在的NSNotificationCenteraddObserverselector打開NSBeginAlertSheet。以下是addObserverselector申報init方法我的主窗口控制器:

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我的樣本項目:https://www.dropbox.com/s/0xfe4bk17v9girj/PanelApplication.zip?dl=0

回答

0

問題不在於與通知,這是這是導致問題的面板: 我在我的主窗口控制器打開面板:

[[self window] beginSheet:self.panelClass.panel completionHandler:nil];

和麪板的關閉行動寫在Panel窗口控制器中。由於這個原因,沒有NSBeginAlertSheet一旦自定義面板加載/卸載在主窗口上不顯示。

[[self window] endSheet:self.panelClass.panel returnCode:NSModalResponseCancel]; [self.panelClass.panel orderOut:self];

0

這是一個有點難以啓齒,但我最好的猜測是,這是反應的通知,在okButtonClicked對象:功能,不具有(有效的)參考你的窗戶。然後,警報不知道要顯示哪個窗口。

如果您可以在通知對象中發送對窗口的引用,那麼您應該能夠在所需的窗口中顯示警報。

示例代碼:

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

和:

- (void) okButtonClicked:(NSNotification*)notification 
{ 
    NSBeginAlertSheet(@"Alert", @"Ok", nil, nil, [notification object], self,nil, nil,nil,@"Alert is being shown on window."); 
} 

這部作品在我的測試項目。

+0

我送一些'userInfo'而發佈所述通知:

因此移動下面的一段從面板窗控制器到主窗控制器代碼解決了這個問題。NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:[sender tag]],@「value」,nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@「PanelButtonClickedNotification」object:self userInfo:dict];所以我不能發送'window'作爲通知對象。 – triandicAnt 2015-02-24 08:24:45

+0

你可以將它添加到你正在創建和解析的字典中。當然,只有當它存在/班級知道這個窗口。我們可以使用NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:[sender tag]],@「value」,[self window],@「window」,nil]; 然後檢索它 [[通知userInfo] objectForKey:@「窗口」] – 2015-02-24 17:39:46

+0

發佈通知時無法傳遞'[self window]',因爲它在單獨的窗口控制器中(在此僅存在一個面板窗口,其上有按鈕,按下這些按鈕時,通知被髮送)。主窗口在另一個窗口控制器中,我正在接收通知...另外,一旦面板在主窗口中打開並且發送/接收通知...之後,我無法在任何其他方法中打開NSBeginAlertSheet: ( – triandicAnt 2015-02-25 04:55:00