macos - Mac 应用程序中的“确定”警报按钮不起作用

标签 macos cocoa cocoa-sheet

单击按钮后,我使用以下代码

testViewController *myWindowController  = [[testViewController alloc] initWithWindowNibName:@"RecordingsViewController"];

[myWindowController setDelegate:self];
activeModalWindow = [myWindowController window];

[NSApp beginSheet:[myWindowController window]
   modalForWindow:[self window]
    modalDelegate:self
   didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) 
      contextInfo:nil];
[NSApp runModalForWindow:[myWindowController window]];

[[myWindowController window] orderOut: self];

从这个 testViewController 中,我使用代码显示警报

NSString *theAlertMessage = [NSString stringWithFormat: @"Already added"];
NSRunAlertPanel(@"", theAlertMessage, @"OK", nil, nil);

但是单击此警报的“确定”按钮后。我的警报仍然显示在屏幕上。请帮忙!

最佳答案

使用它作为:

NSAlert *alert = [[NSAlert alloc] init];
[alert setAlertStyle:2];
[alert setMessageText:@"Already added"];

    [alert beginSheetModalForWindow:[(AppDelegate *)[[NSApplication sharedApplication] delegate] window]
                   modalDelegate:self

                  didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
                     contextInfo:nil];
 }

 - (void)sheetDidEnd:(NSAlert *)alert
               returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
    NSLog(@"clicked %d button\n", returnCode);

}

希望对您有帮助。

关于macos - Mac 应用程序中的“确定”警报按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6661408/

相关文章:

c++ - 在 64 位 mac 上编译 OpenCV 代码

java - 如何使用 JavaFX 在 mac 顶部工具栏上显示菜单?

cocoa - 查找窗口停用时发生的 Cocoa (Mac OS X) 事件

objective-c - 使用 Coca 应用程序中的默认复制菜单项复制 NSTableView 行或列的内容

objective-c - NSPanel 始终位于所有其他应用程序窗口之上

cocoa - 我使用 BeginSheet 正确吗? (单色Mac)

macos - 查找 VimR OSX 的配置文件(vimrc?)

macos - 在 OSX 中打开和关闭网络连接的命令行方法

单击后 Swift 3 立即隐藏 NSButton

cocoa - 模态 session 需要模态窗口