使用Alert和Panel
来源:互联网 发布:软件过程管理解释 编辑:程序博客网 时间:2024/05/22 18:56
一.使用Alert
一个alert基于NSAlert类,最简单创建一个alert的代码:
var myAlert=NSAlert()myAlert.runModal()
为了自定义一个alert,可以修改下面的属性:
- messageText
- informativeText
- icon
- alertStyle
- showsSuppressionButton
- suppressionButton
示例代码:
AppDelegate.swift文件
import Cocoa@NSApplicationMainclass AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! func applicationDidFinishLaunching(aNotification: NSNotification) { } func applicationWillTerminate(aNotification: NSNotification) { } @IBAction func showInfo(sender: AnyObject) { let myAlert=NSAlert() myAlert.messageText="messageText" myAlert.informativeText="informativeText" myAlert.alertStyle=NSAlertStyle.CriticalAlertStyle myAlert.showsSuppressionButton=true myAlert.suppressionButton?.title="下次不再提醒我" myAlert.runModal() }}
运行效果:
从alert中获取反馈信息:
一个alert通常只会展示一个OK按钮,但是一个alert可以从用户那里获取反馈信息,通过以下方式:
- 选择suppression check box
- 点击除了OK按钮的其它按钮
为了确定用户是否选择了suppression check box,你需要获取suppressionButton.state属性;
你可以通过addButtonWithTitle
方法来添加更多的按钮,然后再来确定用户点击了哪个按钮,你需要使用NSAlertFirstButtonReturn
、NSAlertSecondButtonReturn
、NSAlertThirdButtonReturn
常量,如果alert上的按钮超过三个,你可以使用NSAlertThirdButtonReturn+n
来获取按钮。
示例代码:
AppDelegate.swift文件
import Cocoa@NSApplicationMainclass AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! func applicationDidFinishLaunching(aNotification: NSNotification) { } func applicationWillTerminate(aNotification: NSNotification) { } @IBAction func showInfo(sender: AnyObject) { let myAlert=NSAlert() myAlert.messageText="messageText" myAlert.informativeText="informativeText" myAlert.alertStyle=NSAlertStyle.CriticalAlertStyle myAlert.showsSuppressionButton=true myAlert.suppressionButton?.title="下次不再提醒我" myAlert.addButtonWithTitle("第一个") myAlert.addButtonWithTitle("第二个") myAlert.addButtonWithTitle("第三个") myAlert.addButtonWithTitle("第四个") let choice=myAlert.runModal() switch choice { case NSAlertFirstButtonReturn: print("第一个") case NSAlertSecondButtonReturn: print("第二个") case NSAlertThirdButtonReturn: print("第三个") case NSAlertThirdButtonReturn+1: print("第四个") default:break } if myAlert.suppressionButton!.state==1{ print("选中") }else{ print("未选中") } }}
效果如下:
以Sheet形式展示Alert
Alert通常以模态对话框的形式展示,另一种展示alert的方式是Sheet:从当前激活窗口的标题栏滚下
示例代码:
import Cocoa@NSApplicationMainclass AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! func applicationDidFinishLaunching(aNotification: NSNotification) { } func applicationWillTerminate(aNotification: NSNotification) { } @IBAction func showInfo(sender: AnyObject) { let myAlert=NSAlert() myAlert.messageText="messageText" myAlert.informativeText="informativeText" myAlert.alertStyle=NSAlertStyle.CriticalAlertStyle myAlert.showsSuppressionButton=true myAlert.suppressionButton?.title="下次不再提醒我" myAlert.addButtonWithTitle("第一个") myAlert.addButtonWithTitle("第二个") myAlert.addButtonWithTitle("第三个") myAlert.addButtonWithTitle("第四个") let myCode={ (choice:NSModalResponse) -> Void in switch choice { case NSAlertFirstButtonReturn: print("第一个") case NSAlertSecondButtonReturn: print("第二个") case NSAlertThirdButtonReturn: print("第三个") case NSAlertThirdButtonReturn+1: print("第四个") default:break } if myAlert.suppressionButton!.state==1{ print("选中") }else{ print("未选中") } } myAlert.beginSheetModalForWindow(window, completionHandler: myCode) }}
效果:
二.使用Panel(面板)
OS X程序可以显示一个Open面板来让用户选择一个文件或者显示一个Save面板来让用户选择一个文件夹来保存文件,分别基于的类是NSOpenPanel和NSSavePanel
1.创建一个Open面板
Open面板让用户选择一个文件来打开,如果用户选择一个文件的话,需要返回一个文件名,属性如下:
- canChooseFiles
- canChooseDirectories
- allowsMultipleSelection
- URLs
示例代码:
AppDelegate.swift文件
import Cocoa@NSApplicationMainclass AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! func applicationDidFinishLaunching(aNotification: NSNotification) { } func applicationWillTerminate(aNotification: NSNotification) { } @IBAction func openFile(sender: AnyObject) { let myOpen=NSOpenPanel() myOpen.canChooseFiles=true myOpen.canChooseDirectories=true myOpen.allowsMultipleSelection=true myOpen.beginWithCompletionHandler{(result) -> Void in if result == NSFileHandlingPanelOKButton { print(myOpen.URLs) } } }}
效果图:
2.创建Save面板
一个Save面板目的是让用户选择文件夹,定义一个文件名来存储,如果用户选择一个文件的话,返回一个文件名,属性如下:
- title
- prompt
- URL
- nameFieldStringValue
AppDelegate.swift文件
import Cocoa@NSApplicationMainclass AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! func applicationDidFinishLaunching(aNotification: NSNotification) { } func applicationWillTerminate(aNotification: NSNotification) { } @IBAction func openFile(sender: AnyObject) { let mySave=NSSavePanel() mySave.title="保存文件" mySave.prompt="保存我吧" mySave.beginWithCompletionHandler{(result) -> Void in if result == NSFileHandlingPanelOKButton { print(mySave.URL) print(mySave.nameFieldStringValue) } } } }
效果图如下:
- 使用Alert和Panel
- [原创]使用自定义的alert和confirm
- J2ME中List和Alert的使用
- php.echo 和window.alert()配合使用
- alert使用
- 简单控件Panel和PlaceHolder的使用区别
- 简单控件Panel和PlaceHolder的使用区别
- 简单控件Panel和PlaceHolder的使用区别
- 简单控件Panel和PlaceHolder的使用区别
- c# 使用Button和Panel 设计伸缩面板控件
- 使用alert()后,页面和文字变形 asp.net
- 如何使用panel
- 在frame使用panel
- Panel控件的使用
- 使用按钮切换Panel
- easyui panel 使用汇总
- Ext.grid.Panel使用
- Panel的使用
- Redis学习笔记(2)
- 南大软院大神养成计划-网页设计初学HTML
- eclipse集成SVN的几种方式整理
- Android中进程与线程基础知识
- webKit之扩展JSC的三种方法
- 使用Alert和Panel
- C++迭代器之'插入迭代器'
- EhLib控件使用技巧 --DBGridEh自动过滤
- linux几种快速清空文件内容的方法
- mysql使用MRG_MyISAM(MERGE)实现水平分表
- IOS UIScrollView
- linux下开发之makefile文件
- Webkit之JSC Binding解析
- 边做边学Rust之原生数据类型