利用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
原创粉丝点击