我有NSAlert实例,我以取消某些操作的用户确认模式运行。当用户没有响应并且操作完成时,我需要关闭此模态窗口。所以,为此我在alert中调用performClick on default按钮。但我观察到执行点击不会立即执行,但等待一些外部事件,如鼠标移动事件。为什么会这样?除了发布假事件,还有哪些其他解决方案?
答案 0 :(得分: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");
}
答案 1 :(得分:1)
要知道单击了哪个按钮,您可以修改mTo知道单击了哪个按钮,您可以修改方法 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);
}
}
请你详细说明“但是click事件(从performClick生成)本身等待一些外部事件(例如:鼠标移动) - ”