OS X开发.第二章,2.1,NSAlert

2.1 NSAlert
NSAlert是Cocoa中用来进行警告或提醒类的窗口,其类似于JOptionalPane或MessageBox。常用的有两种类型,一种是仅以信息提示,另一种是信息提示辅以事件响应,请看下文介绍。

2.1.1
1.仅信息提示类

    private func showAlert(){
        let alert = NSAlert()
        alert.addButton(withTitle: "OK")
        alert.messageText = "Test Alert"
        alert.informativeText = "This is a concrete information for user."
        alert.alertStyle = .informational
        alert.icon = NSImage(named: "close")
 	 	alert.runModal()
    }

说明:调用NSAlert类我们只需封装一个方法就可以。NSAlert的属性有标题、详细文本信息、按钮文本、样式和显示图片。其中显示图片是非必须的,如果没有设置系统会默认提供一个默认图片;按钮文本最多可添加三个。

2.信息提示辅以事件响应

    private func showAlert(){
        let alert = NSAlert()
        alert.addButton(withTitle: "OK")
        alert.messageText = "Test Alert"
        alert.informativeText = "This is a concrete information for user."
        alert.alertStyle = .informational
        alert.icon = NSImage(named: "close")
        alert.beginSheetModal(for: self.view.window!) { (returnCode) in
            switch returnCode.rawValue{
            case 1000:
                print("------> 1000")
            case 1001:
                print("------> 1000")
            case 1002:
                print("------> 1000")
            default:
            break
            }
        
        }
    }

说明:在beginSheetMoal()中采用闭包将返回码进行回调。1000、1001与1002分别对应生成的三个按钮(本例为了方便仅设置一个Button)的点击回调选项。需要注意的是,仅信息提示的NSAlert弹窗是可以在其弹出后自由移动的,而信息提示辅以事件响应则是NSAlert弹窗顶部与window顶部平齐不可移动的,文末有截图可发现。

运行截图:
信息提示
在这里插入图片描述
信息提示辅以事件响应
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值