源:http://helloitworks.com/863.html

NSAlert用于弹出一个确认对话框,在程序中被广泛地使用。常见的场景是用户删除数据,会弹出对话框给用户确认,免得用户不小心导致了误操作。

NSAlert可以采用Modal Window的方式展示

如图:

代码如下:

  1. //采用Modal Window的方式展示
  2. - (IBAction)ShowNSAlertWindow:(id)sender
  3. {
  4. NSAlert *alert = [NSAlert alertWithMessageText:@"messageText"
  5. defaultButton:@"defaultButton"
  6. alternateButton:@"alternateButton"
  7. otherButton:@"otherButton"
  8. informativeTextWithFormat:@"informativeText"];
  9. NSUInteger action = [alert runModal];
  10. //响应window的按钮事件
  11. if(action == NSAlertDefaultReturn)
  12. {
  13. NSLog(@"defaultButton clicked!");
  14. }
  15. else if(action == NSAlertAlternateReturn )
  16. {
  17. NSLog(@"alternateButton clicked!");
  18. }
  19. else if(action == NSAlertOtherReturn)
  20. {
  21. NSLog(@"otherButton clicked!");
  22. }
  23. }

NSAlert也可以采用Sheet的方式展示

如图:

代码如下:

  1. //采用Sheet的方式展示
  2. - (IBAction)ShowNSAlertSheet:(id)sender
  3. {
  4. NSMutableDictionary * extrasDict = [[NSMutableDictionary alloc] init];
  5. [extrasDict setObject:@"http://www.baidu.com" forKey:@"link"];
  6. NSAlert *alert = [NSAlert alertWithMessageText:@"messageText"
  7. defaultButton:@"defaultButton"
  8. alternateButton:@"alternateButton"
  9. otherButton:@"otherButton"
  10. informativeTextWithFormat:@"informativeText"];
  11. //__bridge_retained for arc
  12. [alert beginSheetModalForWindow:self.window
  13. modalDelegate:self
  14. didEndSelector:@selector(alertSheetDidEnd:returnCode:contextInfo:)
  15. contextInfo:(__bridge void *)(extrasDict )];
  16. }
  17. //响应Sheet的按钮事件
  18. - (void)alertSheetDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
  19. {
  20. if (returnCode == NSAlertDefaultReturn)
  21. {
  22. NSLog(@"alternateButton clicked!");
  23. //show you how to use contextInfo
  24. //__bridge_transfer for arc
  25. NSString *url = [(__bridge NSDictionary*)contextInfo objectForKey:@"link"];
  26. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
  27. }
  28. else if(returnCode == NSAlertAlternateReturn )
  29. {
  30. NSLog(@"alternateButton clicked!");
  31. }
  32. else if(returnCode == NSAlertOtherReturn)
  33. {
  34. NSLog(@"otherButton clicked!");
  35. }
  36. }

源代码:https://github.com/helloitworks/NSAlert

=====================华丽的分割线=====================

可以说NSAlert是标准的,中规中矩,几乎可以应用到所有需要提示框的地方。但我们很难通过继承的方式来扩展NSAlert的功能,事实上NSAlert的设计初衷就是提供一个提示框标准,并不希望用户通过继承去自定义。
在特定的应用程序中,我们经常希望可以自己提供一个自定义窗口,并可以像NSAlert那样采用Modal Window的方式或者采用Sheet的方式来展示。比如黑色主题的程序希望这个NSAlert窗口是黑色的,而不是标准的灰白色,这样才显得和谐。

下面我通过继承NSObject的方式来实现一个SYXAlert类,SYXAlert类采用一个自定义的窗口SYXAlert来模拟NSAlert。

SYXAlert可以采用Modal Window的方式展示

如图:

代码如下:

  1. //采用Window的方式展示
  2. - (IBAction)ShowSYXAlertWindow:(id)sender
  3. {
  4. SYXAlert *alert = [SYXAlert alertWithMessageText:@"SYXAlertWindow" okButton:@"Ok" cancelButton:@"Cancel"];
  5. NSInteger action = [alert runModal];
  6. if(action == SYXAlertOkReturn)
  7. {
  8. NSLog(@"SYXAlertOkButton clicked!");
  9. }
  10. else if(action == SYXAlertCancelReturn )
  11. {
  12. NSLog(@"SYXAlertCancelButton clicked!");
  13. }
  14. }

注:modal对话框窗口左上角是没有Close、Minimize、Resize这些按钮的,所以在xib中去掉这些按钮

SYXAlert也可以采用Sheet的方式展示

如图:

代码如下:

  1. //采用Sheet的方式展示
  2. - (IBAction)ShowSYXAlertSheet:(id)sender
  3. {
  4. NSMutableDictionary * extrasDict = [[NSMutableDictionary alloc] init];
  5. [extrasDict setObject:@"http://www.baidu.com" forKey:@"link"];
  6. SYXAlert *alert = [SYXAlert alertWithMessageText:@"SYXAlertSheet" okButton:@"Ok" cancelButton:@"Cancel"];
  7. [alert beginSheetModalForWindow:self.window
  8. modalDelegate:self
  9. didEndSelector:@selector(alertSheetDidEnd:returnCode:contextInfo:)
  10. contextInfo:(__bridge void*)extrasDict];
  11. }
  12. //响应Sheet的按钮事件
  13. - (void)alertSheetDidEnd:(NSAlert *)alert
  14. returnCode:(NSInteger)returnCode
  15. contextInfo:(void *)contextInfo {
  16. if (returnCode == SYXAlertOkReturn)
  17. {
  18. NSLog(@"SYXAlertOkButton clicked!");
  19. //show you how to use contextInfo
  20. //__bridge_transfer for arc
  21. NSString *url = [(__bridge NSDictionary*)contextInfo objectForKey:@"link"];
  22. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
  23. }
  24. else if(returnCode == SYXAlertCancelReturn )
  25. {
  26. NSLog(@"SYXAlertCancelButton clicked!");
  27. }
  28. }

注:xib的window属性有一个选项,就是visible at launch,默认是勾选,窗口无法采用sheet的方式附在父窗口上;勾掉,窗口才能采用sheet的方式附在父窗口上

源代码:https://github.com/helloitworks/SYXAlert

[MacOS NSAlert的使用]的更多相关文章

  1. MacOS微信逆向分析-Frida

    MacOS微信逆向分析-Frida 0.前言 PC下的微信二次开发相信大家都会了,那么本篇文章将带领大家使用Frida框架对Mac下微信来进行二次开发! PS:还有一种静态注入的方式也不错,但是考虑到 ...

  2. TODO:macOS编译PHP7.1

    TODO:macOS编译PHP7.1 本文主要介绍在macOS上编译PHP7.1,有兴趣的朋友可以去尝试一下. 1.下载PHP7.1源码,建议到PHP官网下载纯净到源码包php-7.1.0.tar.g ...

  3. TODO:macOS上ThinkPHP5和Semantic-UI集成

    TODO:macOS上ThinkPHP5和Semantic-UI集成 1. 全局安装 (on OSX via homebrew)Composer 是 homebrew-php 项目的一部分 2. 把X ...

  4. CoreCRM 开发实录——Travis-CI 实现 .NET Core 程度在 macOS 上的构建和测试 [无水干货]

    上一篇文章我提到:为了使用"国货",我把 Linux 上的构建和测试委托给了 DaoCloud,而 Travis-CI 不能放着不用啊.还好,这货支持 macOS 系统.所以就把 ...

  5. docker4dotnet #3 在macOS上使用Visual Studio Code和Docker开发asp.net core和mysql应用

    .net猿遇到了小鲸鱼,觉得越来越兴奋.本来.net猿只是在透过家里那田子窗看外面的世界,但是看着海峡对岸的苹果园越来越茂盛,实在不想再去做一只宅猿了.于是,.net猿决定搭上小鲸鱼的渡轮到苹果园去看 ...

  6. 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 ...

  7. 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 ...

  8. Asp.Net Core 发布和部署( MacOS + Linux + Nginx )

    前言 在上篇文章中,主要介绍了 Dotnet Core Run 命令,这篇文章主要是讲解如何在Linux中,对 Asp.Net Core 的程序进行发布和部署. 有关如何在 Jexus 中进行部署,请 ...

  9. 在MacOS 10.12上安装Tomcat8.5

    在MacOS 10.12上安装Tomcat8.5 原文链接:https://wolfpaulus.com/journal/mac/tomcat8/ Context 已安装Java,使用java -ve ...

随机推荐

  1. windows mysql提示:1045 access denied for user 'root'@'localhost' using password yes 解决方案

    win7 MySql5.6.17提示:1045 access denied for user 'root'@'localhost' using password yes 从网上找到的解决方法,以此博客 ...

  2. 分享我对领域驱动设计(DDD)的学习成果

    本文内容提要: 1. 领域驱动设计之领域模型 2. 为什么建立一个领域模型是重要的 3. 领域通用语言(Ubiquitous Language) 4.将领域模型转换为代码实现的最佳实践 5. 领域建模 ...

  3. swift邮箱手机验证

    import UIKit class Validate: NSObject { //邮箱.手机验证 enum ValidatedType { case Email case PhoneNumber } ...

  4. JavaScript Array

    1.常用方法 // 数组构造 var a = new Array(20); // 长度为20的数组 var b = new Array('red', 'blue', 'white'); var c = ...

  5. Javascript/jQuery 获取地址栏URL参数的方法

    1.jquery获取url很简单,代码如下 window.location.href; 2.javascript获取url参数 function getUrlParam(name) { var reg ...

  6. CXF集成Spring实现webservice的发布与请求

    CXF集成Spring实现webservice的发布(服务端) 目录结构: 主要代码: package com.cxf.spring.pojo; public class User { int id ...

  7. C#分割字符串

    命名空间:System.String.Split 程序集:mscorlib( mscorlib.dll) 简单实例: string before = "12,50,30"; str ...

  8. Java网络编程——UDP实例

    UDPSendDemo import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea ...

  9. nginx 编译参数详解(运维不得不看)

    nginx参数: --prefix= 指向安装目录 --sbin-path 指向(执行)程序文件(nginx) --conf-path= 指向配置文件(nginx.conf) --error-log- ...

  10. RAP在centos上的部署

    在centos7上部署RAP(非官方) 作者批注:该部署文档为网友贡献,仅供参考.war请参考主页README.md下载最新版本哟~~~ 感谢热情网友的Wiki整理!万分感谢! 系统: centos7 ...