我有兩個不同的窗口控制器。首先是一個自定義面板窗口控制器,另一個是主窗口控制器。面板窗口中有一個面板窗口,該面板上有按鈕。在這些我張貼通知如按鈕的點擊: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];
}
現在,在我的主窗口控制,我想在的NSNotificationCenter
addObserver
的selector
打開NSBeginAlertSheet
。以下是addObserver
selector
申報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
我送一些'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
你可以將它添加到你正在創建和解析的字典中。當然,只有當它存在/班級知道這個窗口。我們可以使用NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:[sender tag]],@「value」,[self window],@「window」,nil]; 然後檢索它 [[通知userInfo] objectForKey:@「窗口」] – 2015-02-24 17:39:46
發佈通知時無法傳遞'[self window]',因爲它在單獨的窗口控制器中(在此僅存在一個面板窗口,其上有按鈕,按下這些按鈕時,通知被髮送)。主窗口在另一個窗口控制器中,我正在接收通知...另外,一旦面板在主窗口中打開並且發送/接收通知...之後,我無法在任何其他方法中打開NSBeginAlertSheet: ( – triandicAnt 2015-02-25 04:55:00