利用Radio Button、Check Box、Date Picker和Slider作出选择
来源:互联网 发布:.cx域名投资 编辑:程序博客网 时间:2024/06/05 07:33
一.使用Check Box:
Check Box是基于NSButton类,允许用户选择多个选项,有三个最重要的属性:
Title
:Check Box附近的文本(未被选中时候的文本)State
:决定Check Box是否被选中(选中为1;未选中为0)Alternate
:Check Box被选中时候的文本
示例代码:
AppDelegate.swift文件
import Cocoa@NSApplicationMainclass AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! @IBOutlet weak var dogBox: NSButton! @IBOutlet weak var catBox: NSButton! @IBOutlet weak var duckBox: NSButton! @IBOutlet weak var messageBox: NSTextField! func applicationDidFinishLaunching(aNotification: NSNotification) { } func applicationWillTerminate(aNotification: NSNotification) { } @IBAction func checkBoxes(sender: NSButton) { let nextLine = "\r\n" var message:String = "" if dogBox.state == 1 { message = "Dog" + nextLine + message }else{ message = "NoDog" + nextLine + message } if catBox.state == 1 { message = "Cat" + nextLine + message }else{ message = "NoCat" + nextLine + message } if duckBox.state == 1 { message = "Duck" + nextLine + message }else{ message = "NoDuck" + nextLine + message } messageBox.stringValue = message }}
效果如图:
二.使用Radio Button
Radio Button只允许用户一次选中一个选项。Xcode7之前在Interface Builder中可以使用Radio Group,但是Xcode7没有Radio Group,可以使用的方法是:How to create NSRadioButton Group in Xcode 7 OSX,即:拖入到界面中的几个Radio Button必须要有相同的父视图和相同的-action方法:
如图,Box作为三个Radio Button的父视图,且它们的-action都是@IBAction func whichBtn(sender: AnyObject)
代码如下:
AppDelegate.swift代码
import Cocoa@NSApplicationMainclass AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! func applicationDidFinishLaunching(aNotification: NSNotification) { } func applicationWillTerminate(aNotification: NSNotification) { } @IBAction func whichBtn(sender: AnyObject) { let myAlert=NSAlert() myAlert.messageText="You click the radio Button with a tag \((sender as! NSButton).tag)" myAlert.runModal() }}
Interface Builder设计界面图如下:
运行效果如下:
三.使用Date Picker
一个Date Picker可以让用户以适当的格式来选择日期,三种类型的外观如下:
- Textual
- Textual with Stepper
- Graphical
示例代码如下:
AppDelegate.swift文件
import Cocoa@NSApplicationMainclass AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! @IBOutlet weak var chooseDate: NSDatePicker! func applicationDidFinishLaunching(aNotification: NSNotification) { // Insert code here to initialize your application } func applicationWillTerminate(aNotification: NSNotification) { } @IBAction func showDate(sender: NSDatePicker) { let myAlert=NSAlert() myAlert.messageText="you choose the date = \(chooseDate.dateValue)" myAlert.runModal() }}
运行效果如下:
四.使用Slider
一个slider可以是竖直或者水平,代表了某个范围的值。
示例代码如下:
AppDelegate.swift文件:
import Cocoa@NSApplicationMainclass AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! @IBOutlet weak var sliderValue: NSTextField! func applicationDidFinishLaunching(aNotification: NSNotification) { } func applicationWillTerminate(aNotification: NSNotification) { } @IBAction func sliderChange(sender: NSSlider) { sliderValue.integerValue=sender.integerValue }}
0 0
- 利用Radio Button、Check Box、Date Picker和Slider作出选择
- Check Box&Radio Button
- MFC--按钮(radio button,check box)
- 鸡啄米vc++2010系列21(按钮控件Button、Radio Button和Check Box)
- 单选按钮Radio Button和复选框Check Box的使用
- VS2010/MFC编程入门之二十一(常用控件:按钮控件Button、Radio Button和Check Box)
- VS2010/MFC编程入门之二十一(常用控件:按钮控件Button、Radio Button和Check Box)
- VS2010/MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)
- VS2013/MFC编程入门之二十一(常用控件:按钮控件Button、Radio Button和Check Box)
- C++ MFC / VS2013 之二十(常用控件:按钮控件Button、Radio Button和Check Box)
- AndroidGUI04:Button(Basic Button,ImageButton, Check Box, Toggle Button, Radio Button)的常用技巧
- AndroidGUI04:Button(Basic Button,ImageButton, Check Box, Toggle Button, Radio Button)的常用技巧
- AndroidGUI04:Button(Basic Button,ImageButton, Check Box, Toggle Button, Radio Button)的常用技巧
- MFC控件使用说明(combo box Button Check Box Radio Button List Control Edit Box Hot Key IP Address....)
- MFC控件使用说明(combo box Button Check Box Radio Button List Control Edit Box Hot Key IP Address)
- MFC控件使用说明(combo box Button Check Box Radio Button List Control Edit Box Hot Key IP Address....)
- MFC控件使用说明(combo box Button Check Box Radio Button List Control Edit Box Hot Key IP Address....)
- MFC控件使用说明(combo box Button Check Box Radio Button List Control Edit Box Hot Key IP Address....)
- 二级指针的小应用(在主函数中获取子函数的地址)
- bash: ulimit: open files: cannot modify limit: Operation not permitted
- SQL-删除重复数据
- android 源码详解
- JSP(JavaServer Pages)
- 利用Radio Button、Check Box、Date Picker和Slider作出选择
- hdu1254 推箱子
- 内存计数器实现限频
- 循环结构
- SDWebImage加进度条展示图片下载进度
- 026.LinkedList底层详解
- easy-size控件自适应大小的使用
- vector中存放自定义类 ,对类的要求
- 【大话GSM】WCDMA和LTE在提供数据和语音业务上的差异