On a button click, I am using the below code
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];
From this testViewController, I am showing an alert using the code
NSString *theAlertMessage = [NSString stringWithFormat:开发者_如何转开发 @"Already added"];
NSRunAlertPanel(@"", theAlertMessage, @"OK", nil, nil);
But on clicking ok button of this alert. My alert remains on screen. Please help!
Use this to as :
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);
}
Hope it helps you.
Found an alternative, it works perfectly
NSString *theAlertMessage = [NSString stringWithFormat: @"Already added."];
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setAlertStyle:NSCriticalAlertStyle];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:theAlertMessage];
精彩评论