0

我有 NSAlert 实例,我作为模态运行以确认用户取消某些操作。当用户没有响应并且操作完成时,我需要关闭此模式窗口。所以,为此,我在警报中调用 performClick 默认按钮。但我观察到执行单击不会立即执行,而是等待一些外部事件,例如鼠标移动事件。为什么会这样?除了发布虚假事件之外,其他解决方案是什么?

4

2 回答 2

3

这是您需要做的。

Assumption:
1. IBAction is connect to NSButton Which will display the Alert View after clicking upon it.
2. It will perform Click operation by itself on the Second button of the Alert View.

希望下面的代码对你有帮助......

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


- (void)errorAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
    NSLog(@"TEST");
}
于 2013-05-28T06:42:52.703 回答
1

要知道点击了哪个按钮可以修改m要知道点击了哪个按钮可以修改方法errorAltertDidEnd

- (void)errorAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{

   if(returnCode == NSAlertAlternateReturn)
   {
       NSLog(@"TEST Alternate %ld",returnCode);
   }

   if(returnCode == NSAlertDefaultReturn)
   {
       NSLog(@"TEST Default %ld",returnCode);
   }        
    if(returnCode == NSAlertOtherReturn)
    {
        NSLog(@"Test Other %ld",returnCode);
    }    
}

您能否详细说明一下“但是单击事件(从 performClick 生成)本身等待一些外部事件(例如:鼠标移动)-”

于 2013-05-28T07:20:58.070 回答