Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何以编程方式取消窗口的NSAlert运行模式_Objective C_Cocoa - Fatal编程技术网

Objective c 如何以编程方式取消窗口的NSAlert运行模式

Objective c 如何以编程方式取消窗口的NSAlert运行模式,objective-c,cocoa,Objective C,Cocoa,线程1运行模式警报: - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo; { [sheet orderOut:self]; }; -(void)newAlert { currentAlert=[NSAlert alertWithMessageText:@"Switch drives!" defaultButton:@"Cancel syn

线程1运行模式警报:

- (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
{
[sheet orderOut:self];
};


-(void)newAlert
{
currentAlert=[NSAlert alertWithMessageText:@"Switch drives!" defaultButton:@"Cancel sync" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Please switch disks: %@",reason,nil];
[currentAlert setDelegate:self];
[currentAlert beginSheetModalForWindow:self.window completionHandler:^(NSInteger returnCode)
            {
                mustStop=TRUE;
            }];

}     
在其他地方,在另一个线程上,我想取消警报,但这不起作用:

[NSApp endSheet:[self window]];

它不起作用。

您不能在后台线程上执行GUI操作。你必须在主线程上执行这些操作。因此,你可以:

dispatch_async(dispatch_get_main_queue(), ^{
    [NSApp endSheet:self.window];
});
但是,从技术上讲,您应该在
NSWindow
上使用新的sheet方法来实现此目的。因此,你应该:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.window endSheet:currentAlert.window];
});
当然,这意味着您需要跟踪
-newAlert
方法之外的警报。(如果您没有跟踪警报,我想您可以使用
self.window.attachedSheet
,尽管可能存在后台线程取消与警报不同的工作表的竞争条件。)

另外,当使用
-beginSheetModalForWindow:completionHandler:
运行警报时,不使用方法
-didEndSheet:returnCode:contextInfo: