2013-08-27 63 views
3

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

回答

5

下面的代码将帮助您

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

}

+0

谢谢,但我不想加警惕任何按钮。你能告诉我如何在调用另一个函数时关闭nsalert吗? –

+0

这对我有效:[self.myAlert.window orderOut:self]; [self.myAlert.window close]; – lifjoy