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的另一种弹窗,如图所示
下回再讲
分类:
mac os开发笔记
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· Windows 提权-UAC 绕过