2011-07-12 75 views
0

點擊一個按鈕,我使用下面的代碼在Mac應用程序警報OK按鈕不起作用

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

但點擊此提醒的確定按鈕。我的警報仍在屏幕上。請幫忙!

+0

奇怪,這對我有用,你在調試器控制檯中是否有錯誤/警告信息? –

+0

沒有錯誤/警告 – Swastik

+0

你可以請嘗試用簡單的框架使用NSRunAlertPanel來構建新的項目,以驗證其系統中的功能? (爲了排除這個問題只在這個項目中出現的可能性) –

回答

0

找到替代方案,它完美

NSString *theAlertMessage = [NSString stringWithFormat: @"Already added."]; 
NSAlert *alert = [[[NSAlert alloc] init] autorelease]; 
[alert setAlertStyle:NSCriticalAlertStyle]; 
[alert addButtonWithTitle:@"OK"]; 
[alert setMessageText:theAlertMessage]; 
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); 

} 

希望它可以幫助你。