macOS开发 提示窗NSAlert的用法(持续更新中)

 

Mac开发-如何弹出提示窗口

   NSAlert *alert = [[NSAlert alloc] init];
        alert.messageText = @"主标题";
        alert.informativeText = @“我来了”t;//内容
        [alert addButtonWithTitle:@"确定"];//按钮所显示的文案
        [alert runModal];
        //注意,以上操作需要在主线程中进行,否则会报错

 最简单的提示窗

要控制按钮,增加按钮。如图

 

 

复制代码
   NSAlert *alert = [[NSAlert alloc] init];
    alert.alertStyle = NSAlertStyleWarning;
    [alert addButtonWithTitle:TBLocalizedString(@"确定")];

      [alert addButtonWithTitle:TBLocalizedString(@"取消")];

    alert.messageText = TBLocalizedString(@"提示");
    alert.informativeText = TBLocalizedString(@"订单指令已提交");
    // 方式1
[alert beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse returnCode) {
        if (returnCode == NSAlertFirstButtonReturn) {
            NSLog(@"确定");

        } else if (returnCode == NSAlertSecondButtonReturn) {
            NSLog(@"取消");

        } else {
            NSLog(@"else");
        }
    }];
/*! 方式2
NSModalResponse response = [alert runModal];
if (response == NSModalResponseStop) {

}
if (response == NSModalResponseAbort) {

}
if (response == NSModalResponseContinue) {

}
*/
复制代码

 

其中alertStyle,变换可以切换状态如图

 

把上面的代码中这一行换成这个状态 

 alert.alertStyle = NSAlertStyleWarning;

替换

 alert.alertStyle = NSAlertStyleCritical;

就是上图的效果了

控制上面按钮的还有方式二

复制代码
/*! 方式2
NSModalResponse response = [alert runModal];
if (response == NSModalResponseStop) {

}
if (response == NSModalResponseAbort) {

}
if (response == NSModalResponseContinue) {

}
*/
复制代码

使用方式1的时候需要注意的是window不能为nil,不要讲NSAlert添加在HomeVC的ViewDidLoad中,此时Window还没创建成功。

macOS的另一种弹窗,如图所示

 

 下回再讲

posted on   高彰  阅读(1760)  评论(1编辑  收藏  举报

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· Windows 提权-UAC 绕过
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示