在Mac应用程序中警报的确定按钮不起作用

时间:2011-07-12 08:28:45

标签: 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);

但点击此警报的确定按钮。我的提醒仍在屏幕上。请帮忙!

2 个答案:

答案 0 :(得分:1)

将此用作:

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);

}

希望它对你有所帮助。

答案 1 :(得分:0)

找到一个替代方案,它完美地运作

NSString *theAlertMessage = [NSString stringWithFormat: @"Already added."];
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setAlertStyle:NSCriticalAlertStyle];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:theAlertMessage];