[MacOS NSAlert的使用]
源:http://helloitworks.com/863.html
NSAlert用于弹出一个确认对话框,在程序中被广泛地使用。常见的场景是用户删除数据,会弹出对话框给用户确认,免得用户不小心导致了误操作。
NSAlert可以采用Modal Window的方式展示
如图:
代码如下:
- //采用Modal Window的方式展示
- - (IBAction)ShowNSAlertWindow:(id)sender
- {
- NSAlert *alert = [NSAlert alertWithMessageText:@"messageText"
- defaultButton:@"defaultButton"
- alternateButton:@"alternateButton"
- otherButton:@"otherButton"
- informativeTextWithFormat:@"informativeText"];
- NSUInteger action = [alert runModal];
- //响应window的按钮事件
- if(action == NSAlertDefaultReturn)
- {
- NSLog(@"defaultButton clicked!");
- }
- else if(action == NSAlertAlternateReturn )
- {
- NSLog(@"alternateButton clicked!");
- }
- else if(action == NSAlertOtherReturn)
- {
- NSLog(@"otherButton clicked!");
- }
- }
NSAlert也可以采用Sheet的方式展示
如图:
代码如下:
- //采用Sheet的方式展示
- - (IBAction)ShowNSAlertSheet:(id)sender
- {
- NSMutableDictionary * extrasDict = [[NSMutableDictionary alloc] init];
- [extrasDict setObject:@"http://www.baidu.com" forKey:@"link"];
- NSAlert *alert = [NSAlert alertWithMessageText:@"messageText"
- defaultButton:@"defaultButton"
- alternateButton:@"alternateButton"
- otherButton:@"otherButton"
- informativeTextWithFormat:@"informativeText"];
- //__bridge_retained for arc
- [alert beginSheetModalForWindow:self.window
- modalDelegate:self
- didEndSelector:@selector(alertSheetDidEnd:returnCode:contextInfo:)
- contextInfo:(__bridge void *)(extrasDict )];
- }
- //响应Sheet的按钮事件
- - (void)alertSheetDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
- {
- if (returnCode == NSAlertDefaultReturn)
- {
- NSLog(@"alternateButton clicked!");
- //show you how to use contextInfo
- //__bridge_transfer for arc
- NSString *url = [(__bridge NSDictionary*)contextInfo objectForKey:@"link"];
- [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
- }
- else if(returnCode == NSAlertAlternateReturn )
- {
- NSLog(@"alternateButton clicked!");
- }
- else if(returnCode == NSAlertOtherReturn)
- {
- NSLog(@"otherButton clicked!");
- }
- }
源代码:https://github.com/helloitworks/NSAlert
=====================华丽的分割线=====================
可以说NSAlert是标准的,中规中矩,几乎可以应用到所有需要提示框的地方。但我们很难通过继承的方式来扩展NSAlert的功能,事实上NSAlert的设计初衷就是提供一个提示框标准,并不希望用户通过继承去自定义。
在特定的应用程序中,我们经常希望可以自己提供一个自定义窗口,并可以像NSAlert那样采用Modal Window的方式或者采用Sheet的方式来展示。比如黑色主题的程序希望这个NSAlert窗口是黑色的,而不是标准的灰白色,这样才显得和谐。
下面我通过继承NSObject的方式来实现一个SYXAlert类,SYXAlert类采用一个自定义的窗口SYXAlert来模拟NSAlert。
SYXAlert可以采用Modal Window的方式展示
如图:
代码如下:
- //采用Window的方式展示
- - (IBAction)ShowSYXAlertWindow:(id)sender
- {
- SYXAlert *alert = [SYXAlert alertWithMessageText:@"SYXAlertWindow" okButton:@"Ok" cancelButton:@"Cancel"];
- NSInteger action = [alert runModal];
- if(action == SYXAlertOkReturn)
- {
- NSLog(@"SYXAlertOkButton clicked!");
- }
- else if(action == SYXAlertCancelReturn )
- {
- NSLog(@"SYXAlertCancelButton clicked!");
- }
- }
注:modal对话框窗口左上角是没有Close、Minimize、Resize这些按钮的,所以在xib中去掉这些按钮
SYXAlert也可以采用Sheet的方式展示
如图:
代码如下:
- //采用Sheet的方式展示
- - (IBAction)ShowSYXAlertSheet:(id)sender
- {
- NSMutableDictionary * extrasDict = [[NSMutableDictionary alloc] init];
- [extrasDict setObject:@"http://www.baidu.com" forKey:@"link"];
- SYXAlert *alert = [SYXAlert alertWithMessageText:@"SYXAlertSheet" okButton:@"Ok" cancelButton:@"Cancel"];
- [alert beginSheetModalForWindow:self.window
- modalDelegate:self
- didEndSelector:@selector(alertSheetDidEnd:returnCode:contextInfo:)
- contextInfo:(__bridge void*)extrasDict];
- }
- //响应Sheet的按钮事件
- - (void)alertSheetDidEnd:(NSAlert *)alert
- returnCode:(NSInteger)returnCode
- contextInfo:(void *)contextInfo {
- if (returnCode == SYXAlertOkReturn)
- {
- NSLog(@"SYXAlertOkButton clicked!");
- //show you how to use contextInfo
- //__bridge_transfer for arc
- NSString *url = [(__bridge NSDictionary*)contextInfo objectForKey:@"link"];
- [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
- }
- else if(returnCode == SYXAlertCancelReturn )
- {
- NSLog(@"SYXAlertCancelButton clicked!");
- }
- }
注:xib的window属性有一个选项,就是visible at launch,默认是勾选,窗口无法采用sheet的方式附在父窗口上;勾掉,窗口才能采用sheet的方式附在父窗口上
源代码:https://github.com/helloitworks/SYXAlert
[MacOS NSAlert的使用]的更多相关文章
- MacOS微信逆向分析-Frida
MacOS微信逆向分析-Frida 0.前言 PC下的微信二次开发相信大家都会了,那么本篇文章将带领大家使用Frida框架对Mac下微信来进行二次开发! PS:还有一种静态注入的方式也不错,但是考虑到 ...
- TODO:macOS编译PHP7.1
TODO:macOS编译PHP7.1 本文主要介绍在macOS上编译PHP7.1,有兴趣的朋友可以去尝试一下. 1.下载PHP7.1源码,建议到PHP官网下载纯净到源码包php-7.1.0.tar.g ...
- TODO:macOS上ThinkPHP5和Semantic-UI集成
TODO:macOS上ThinkPHP5和Semantic-UI集成 1. 全局安装 (on OSX via homebrew)Composer 是 homebrew-php 项目的一部分 2. 把X ...
- CoreCRM 开发实录——Travis-CI 实现 .NET Core 程度在 macOS 上的构建和测试 [无水干货]
上一篇文章我提到:为了使用"国货",我把 Linux 上的构建和测试委托给了 DaoCloud,而 Travis-CI 不能放着不用啊.还好,这货支持 macOS 系统.所以就把 ...
- docker4dotnet #3 在macOS上使用Visual Studio Code和Docker开发asp.net core和mysql应用
.net猿遇到了小鲸鱼,觉得越来越兴奋.本来.net猿只是在透过家里那田子窗看外面的世界,但是看着海峡对岸的苹果园越来越茂盛,实在不想再去做一只宅猿了.于是,.net猿决定搭上小鲸鱼的渡轮到苹果园去看 ...
- ASP.NET Core 中文文档 第二章 指南(1)用 Visual Studio Code 在 macOS 上创建首个 ASP.NET Core 应用程序
原文:Your First ASP.NET Core Application on a Mac Using Visual Studio Code 作者:Daniel Roth.Steve Smith ...
- Swift 3 and OpenGL on Linux and macOS with GLFW
https://solarianprogrammer.com/2016/11/19/swift-opengl-linux-macos-glfw/ Swift 3 and OpenGL on Linux ...
- Asp.Net Core 发布和部署( MacOS + Linux + Nginx )
前言 在上篇文章中,主要介绍了 Dotnet Core Run 命令,这篇文章主要是讲解如何在Linux中,对 Asp.Net Core 的程序进行发布和部署. 有关如何在 Jexus 中进行部署,请 ...
- 在MacOS 10.12上安装Tomcat8.5
在MacOS 10.12上安装Tomcat8.5 原文链接:https://wolfpaulus.com/journal/mac/tomcat8/ Context 已安装Java,使用java -ve ...
随机推荐
- #CSDN刷票门# 有没有人在恶意刷票?CSDN请告诉我!用24小时监控数据说话!
特别声明: 此次并非针对其他参与2013中国十大优秀开源项目的同行,体系有漏洞要谴责的是制定规则并从中获益但不作为的权贵,草根们制定不了规则但可发现和利用漏洞,这是程序员应有反叛精神没错.但被作为道具 ...
- BASH 命令以及使用方法小结
最近工作中需要写一个Linux脚本,用到了很多BASH命令,为了防止以后忘记,在这里把它们一一记下来.可能会比较乱,随便看看就好了.如果有说的不对的地方也欢迎大家指正. 1,export VAR=.. ...
- Lucene.Net的服务器封装+APi组件 (开源)
为什么要封装 真不知道用什么标题合适,我这几天在研究Lucene.Net,觉得把Lucene.Net封装为一个独立的服务器,再提供一个给客户端调用的Api组件应该是一件很意思的事,主要优势有以下: 1 ...
- .Net简单图片系统-简介
系统简介 最近做了一个简单图片系统,这个系统就是 将上传的的图片保存到系统本地文件系统或者基于fastdfs的分布式文件系统中,在查看图片时会直接请求此系统或者fastdfs的tracker服务器(需 ...
- eclipse汉化全程
在开始之前我说一下我的环境,eclipse版本eclipse-java-indigo-SR2-win32-x86_64,操作系统Win7,但是这个基本上没有影响.红字的那个注意一下,在下面需要根据这个 ...
- [BZOJ1263][SCOI2006]整数划分(数学+高精度)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1263 分析:数学老师上课讲过啦= =,就是尽可能3越多越好.然后就写个高精度就行了.
- 如何解决Windows 10系统下设备的声音问题
如何解决Windows 10系统下设备的声音问题? 请阅读下面的说明来解决Windows 10设备上的声音问题. 1. 检查设备管理器 打开开始菜单,键入设备管理器, 从出现的结果中选择并打开它. 在 ...
- 详解C语言的htons和htonl函数、大尾端、小尾端
在Linux和Windows网络编程时需要用到htons和htonl函数,用来将主机字节顺序转换为网络字节顺序. 在Intel机器下,执行以下程序 int main(){ printf(" ...
- SD卡状态广播
SD状态发生改变的时候会对外发送广播.SD卡的状态一般有挂载.未挂载和无SD卡. 清单文件 一个广播接受者可以接受多条广播.这里在意图过滤器中添加是data属性是因为广播也需要进行匹配的.对方发送的广 ...
- 东大OJ-1430-PrimeNumbers
题目描述 I'll give you a number , please tell me how many different prime factors in this number. 输入 The ...