【发布时间】:2015-02-24 05:18:25
【问题描述】:
我有两个不同的窗口控制器。第一个是自定义面板窗口控制器,另一个是主窗口控制器。在面板窗口中有一个面板窗口,该面板上有按钮。单击这些按钮后,我将发布通知,例如:
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];
}
现在,在我的主窗口控制器中,我试图在 addObserver 的 selector 和 NSNotificationCenter 中打开一个 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.");
}
}
}
当用户单击 Panel 上的 No 按钮时,应在窗口上显示警报。但从未显示警报。我还尝试了[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
【问题讨论】:
标签: objective-c cocoa nsnotificationcenter alerts