如何在一段时间后关闭警报并每 10 分钟重复一次

How to close an alert after some time and repeat it every 10 min

在我的 MAC OSX 应用程序中。我正在弹出一个警报,要求用户 select 是或否。如果用户没有点击任何选项,可能会将其拖到某个角落。所以我想在一段时间后自动关闭它并再次显示相同的警报。所以我可以确保他采取同样的行动。 我使用的警报代码是

-(bool)VpnStatusUnableToConnect:(NSString *)alertMessage
{
    if (nil != alertMessage) {
        NSImage *alertIcon = [NSImage imageNamed:@"dock-alert"]; //my custom image placed in support files
        NSAlert *alert = [[NSAlert alloc]init];
        [alert addButtonWithTitle:@"Try Again"];
        [alert addButtonWithTitle:@"Cancel"];
        [alert setMessageText:alertMessage];
        [alert setAlertStyle:NSWarningAlertStyle];
        [alert setIcon:alertIcon];
        [[alert window] setTitle:@"VPN Connection Status"];
        [[alert window] setBackgroundColor: NSColor.whiteColor];
        if ( [alert runModal] ==  NSAlertFirstButtonReturn)
        {
            return 1;
        }
        else
            return 0;
    }
    return 0;

}

按如下方式修改您的代码并尝试一下

-(void)yourAlert{
    NSAlert *alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle: @"OK"];
    [alert setMessageText: @"Attention!!! This a critical Alert."];
    [alert setAlertStyle: NSInformationalAlertStyle];

    NSTimer *myTimer = [NSTimer timerWithTimeInterval:3
                                               target:self
                                             selector: @selector(killWindow:)
                                             userInfo:nil
                                              repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSModalPanelRunLoopMode];

    int choice = 0;
    choice = [alert runModal];
    if(choice != 0)
        [myTimer invalidate];
}

-(void) killWindow:(NSAlert *)alert with:(NSTimer *) theTimer;
    {

        NSLog(@"killWindow");
        [[alert window] abortModal];

    }