NSAlert

作者: yxibng | 来源:发表于2020-12-27 22:50 被阅读0次

注意事项:

执行run Modal 会block 主线程
等alert 退出modal 状态之后,主线程恢复运行。
modal 期间,抛给主线程的任务全被block住,等线程恢复之后才继续执行。

- (IBAction)displayAsSheet:(id)sender {
    NSAlert *alert = [[NSAlert alloc] init];
    alert.alertStyle = NSAlertStyleCritical;
    alert.icon = [NSImage imageNamed:@"icon.001"];
    alert.messageText = @"This is message text";
    alert.informativeText = @"This is informative text";
    [alert addButtonWithTitle:@"first button"];
    [alert addButtonWithTitle:@"second button"];
    [alert addButtonWithTitle:@"third button"];
    [alert beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse returnCode) {
       
        if (returnCode == NSAlertFirstButtonReturn) {
            NSLog(@"%s, first clicked", __FUNCTION__);
        } else if (returnCode == NSAlertSecondButtonReturn) {
            NSLog(@"%s, second clicked", __FUNCTION__);
        } else {
            NSLog(@"%s, third clicked", __FUNCTION__);
        }
    }];
}

- (IBAction)runModalTestMainThread:(id)sender {
    
    NSAlert *alert = [[NSAlert alloc] init];
    alert.alertStyle = NSAlertStyleWarning;
    alert.icon = [NSImage imageNamed:@"icon.001"];
    alert.messageText = @"This is message text";
    alert.informativeText = @"This is informative text";
    [alert addButtonWithTitle:@"first button"];
    [alert addButtonWithTitle:@"second button"];
    [alert addButtonWithTitle:@"third button"];
    
    for (NSButton *button in alert.buttons) {
        [button setTarget:self];
        [button setAction:@selector(rz_runModalWithSender:)];
    }
    /*
     执行run Modal 会block 主线程
     等alert 退出modal 状态之后,主线程恢复运行。
     modal 期间,抛给主线程的任务全被block住,等线程恢复之后才继续执行。
     */
    [alert runModal];
    /*
     主线程恢复运行
     */
    NSLog(@"main thread resume");
}

- (void)rz_runModalWithSender:(NSButton *)sender {
    
    //参考 https://stackoverflow.com/questions/48688574/programmatically-dismiss-modal-dialog-in-macos
    NSLog(@"%s, title = %@",__FUNCTION__, sender.title);
    [[NSApplication sharedApplication] abortModal];
    
}

参考demo: NSAlert

相关文章

  • Mac osx开发 NSAlert

    osx开发,NSAlert的使用 下面是代码 NSAlert *alert = [[NSAlert alloc]...

  • NSAlert

    1.改大小(x,y动不了,高可以改)https://www.jianshu.com/p/8ce2a26cca40 ...

  • NSAlert

    https://blog.csdn.net/lovechris00/article/details/78024688

  • NSAlert

    注意事项: 执行run Modal 会block 主线程等alert 退出modal 状态之后,主线程恢复运行。m...

  • MAC:NSAlert

    ps:NSAlert 会卡住主线程

  • Mac OS 中 NSAlert的使用

    最近需要做出一个效果,带图片的弹窗。 最开始想到的就是NSAlert 一开始想改变NSAlert的大小去插入图片,...

  • Mac OSX - tips

    1:在模态窗口中弹出NSAlert提示框,NSAlert提示框消失后依然阻塞当前模态窗口,上面的类似输入框等鼠标事...

  • NSAlert 详解

  • MAC开发-NSAlert的简单用法

    开发当中NSAlert,在删除消息,退出登录等场景中会用到。代码如下:

  • macOS开发-NSAlert

    macOS桌面开发NSAlert的使用 1 简述 附加到窗口的模式对话框或工作表,可以定义标题,描述详情,图标,按...

网友评论

      本文标题:NSAlert

      本文链接:https://www.haomeiwen.com/subject/jrjinktx.html