我决定使用带有2个按钮的警报表。当用户单击“继续”按钮时,窗口制作的纸张应该会关闭。工作表向下,父窗口与另一个工作表一起关闭。我正在使用的代码是:
- (void)alertDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(int *)contextInfo
{
if (returnCode == kOkayButtonCode) {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString *status = [defaults objectForKey:@"userStatus"];
if (status == @"NO") {
[NSApp beginSheet:theSheet modalForWindow:window
modalDelegate:self didEndSelector:NULL contextInfo:nil];
}
if (status == @"YES") {
}
}
if (returnCode == kCancelButtonCode) {
[NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.45];
}
}
有人能看到这个问题吗?
答案 0 :(得分:1)
找到了一个使用计时器的解决方法。
- (void)alertDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(int *)contextInfo
{
if (returnCode == kOkayButtonCode) {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString *status = [defaults objectForKey:@"userStatus"];
if (status == @"NO") {
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0.45];
NSTimer *theTimer = [[NSTimer alloc] initWithFireDate:date
interval:1
target:self
selector:@selector(startSheet:)
userInfo:nil repeats:NO];
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:theTimer forMode: NSDefaultRunLoopMode];
[timer2 release];
}
if (status == @"YES") {
}
}
if (returnCode == kCancelButtonCode) {
[NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.45];
}
}
- (void)startSheet:(NSTimer *)theTimer {
[NSApp beginSheet:theSheet modalForWindow:window
modalDelegate:self didEndSelector:NULL contextInfo:nil];
}