如何在OSX上自动关闭NSAlert?

Jos*_*iel 3 macos cocoa objective-c

我想创建NSAlert(弹出窗口)show然后自动关闭.它同样点击按钮,它显示弹出扫描...,找到任何项目后,弹出扫描自动关闭.当弹出窗口显示时,用户无法单击我的应用程序上的任何按钮.我怎样才能做到这一点?非常感谢.

小智 7

下面的代码将帮助您

- (IBAction)showAlert:(id)sender {
    //display the alert
    self.myAlert = [NSAlert alertWithMessageText:@"Sample Test" defaultButton:@"OK" alternateButton:@"DO Nothing" otherButton:@"CANCEL" informativeTextWithFormat:@"TEST",nil];
    [self.myAlert beginSheetModalForWindow:[self window]
                         modalDelegate:self
                        didEndSelector:@selector(errorAlertDidEnd:returnCode:contextInfo:)
                           contextInfo:nil];

    NSArray *buttonArray = [self.myAlert buttons];
    NSLog(@"Button Arrays %@",buttonArray);

    //Close by itself without a mouse click by the user
    //Assuming the Default Button as the Second one "Do Nothing
    NSButton *myBtn = [buttonArray objectAtIndex:2];
    [myBtn performClick:self.myAlert];
Run Code Online (Sandbox Code Playgroud)

}