objective-c - cocoa 执行点击按钮等待下一个外部事件

标签 objective-c cocoa user-interface modal-dialog

我有 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/

相关文章:

objective-c - Cocoa - 暂停一个方法

swift - 在 Swift 中使用 UnsafeMutablePointer 编码和解码字符串

cocoa - 禁用 webview 键盘等效项

c# - UserControl 在放置在窗体上时膨胀?

android - 适配器作为 Activity 的内部类,还是独立的类?

android - 在 Android 线程上更改图像

ios - UIDatePicker 设置 UITextfield 文本

ios - 当前导航 Controller 来自另一个导航 Controller

objective-c - 如何在 Cocoa for OSX 中显示多个图像?

ios - 使用终端中的检测自动化 iOS 内存泄漏检测