【问题标题】:How to close an alert after some time and repeat it every 10 min如何在一段时间后关闭警报并每 10 分钟重复一次
【发布时间】:2016-10-18 21:54:38
【问题描述】:

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

-(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;

}

【问题讨论】:

    标签: macos nsalert


    【解决方案1】:

    如下修改你的代码并试一试

    -(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];
    
        }
    

    【讨论】:

    • 这是一个非常聪明和简单的方法来解决 runModal 的整体块状问题。如果允许,我会给你两次投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 2023-03-16
    • 1970-01-01
    • 2012-02-08
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多