开发者

ok button for alert in mac app does not work

开发者 https://www.devze.com 2023-03-19 20:33 出处:网络
On a button click, I am using the below code testViewController *myWindowController= [[testViewController alloc] initWithWindowNibName:@\"RecordingsViewController\"];

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消