我有 NSAlert 实例,我将其作为用户确认取消某些操作的模式运行。当用户没有响应并且操作完成时,我需要关闭此模式窗口。因此,为此我在警报中的默认按钮上调用执行单击。但我观察到执行单击不会立即执行,而是等待一些外部事件,例如鼠标移动事件。为什么会发生这种情况?除了发布虚假事件外,还有哪些解决方案?
最佳答案
这是您需要执行的操作。
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");
}
关于objective-c - cocoa 执行点击按钮等待下一个外部事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16783608/