1,选择框可以让用户以滑动的方式选择值。示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import UIKit
 
class ViewController:UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
     
    var pickerView:UIPickerView!
     
    override func viewDidLoad() {
        super.viewDidLoad()
        pickerView=UIPickerView()
        //将dataSource设置成自己
        pickerView.dataSource=self
        //将delegate设置成自己
        pickerView.delegate=self
        //设置选择框的默认值
        pickerView.selectRow(1,inComponent:0,animated:true)
        pickerView.selectRow(2,inComponent:1,animated:true)
        pickerView.selectRow(3,inComponent:2,animated:true)
        self.view.addSubview(pickerView)
         
        //建立一个按钮,触摸按钮时获得选择框被选择的索引
        var button=UIButton(frame:CGRectMake(0,0,100,30))
        button.center=self.view.center
        button.backgroundColor=UIColor.blueColor()
        button.setTitle("获取信息",forState:.Normal)
        button.addTarget(self, action:"getPickerViewValue",
            forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button)
    }
    //设置选择框的列数为3列,继承于UIPickerViewDataSource协议
    func numberOfComponentsInPickerView( pickerView: UIPickerView) -> Int{
        return 3
    }
     
    //设置选择框的行数为9行,继承于UIPickerViewDataSource协议
    func pickerView(pickerView: UIPickerView,numberOfRowsInComponent component: Int) -> Int{
        return 9
    }
     
    //设置选择框各选项的内容,继承于UIPickerViewDelegate协议
    func pickerView(pickerView:UIPickerView!,titleForRow row: Int,forComponent component: Int)
        -> String!{
        return String(row)+"-"+String(component)
    }
     
    //触摸按钮时,获得被选中的索引
    func getPickerViewValue(){
        var alertView=UIAlertView();
        alertView.title="被选中的索引为"
        alertView.message=String(pickerView.selectedRowInComponent(0))+"-"+String(pickerView!.selectedRowInComponent(1))+"-"+String(pickerView.selectedRowInComponent(2))
        alertView.addButtonWithTitle("OK")
        alertView.show()
    }
}

2,调整选择框的尺寸
UIPickerView用frame和center两个属性设置整个选择框的大小和位置。
如果要调整内部列的宽度,需要实现UIPickerViewDelegate协议类中pickerView:widthForComponent方法设置
如果要调整内部行高,则需要实习上述协议类中pickerView:rowHeightForComponent方法设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//设置列宽
func pickerView(pickerView: UIPickerView!,widthForComponent component: Int) -> CGFloat{
    if(00 == component){
        //第一列变宽
        return 100
    }else{
        //第二、三列变窄
        return 30
    }
}
 
//设置行高
func pickerView(pickerView: UIPickerView!,rowHeightForComponent component: Int) -> CGFloat{
    return 50
}

3,将图片作为选择框选项
选择框选项的内容,除了可以使字符串类型的,还可以是任意UIView类型的元素。比如我们将选项内容设置为图片:

1
2
3
4
5
6
7
func pickerView(pickerView:UIPickerView!,viewForRow row: Int,forComponent component: Int,
 reusingView view:UIView!) -> UIView!{
    var image = UIImage(named:"icon_"+String(row))
    var imageView = UIImageView()
    imageView.image = image
    return imageView
}

4,检测响应选项的选择状态

1
2
3
4
5
func pickerView(pickerView: UIPickerView!,didSelectRow row: Int, inComponent component: Int){
    //将在滑动停止后触发,并打印出选中列和行索引
    println(component)
    println(row)
}

Swift - 选择框(UIPickerView)的用法的更多相关文章

  1. 下拉选择框 Spinner的用法。

    代码如下: package com.lixu.xialakuang; import android.app.Activity; import android.content.Context; impo ...

  2. swift 实践- 12 -- UIPickerView

    import UIKit class ViewController: UIViewController , UIPickerViewDelegate,UIPickerViewDataSource{ v ...

  3. select2 选择框插件

    <select id="selBusi_type"><select> //初始化业务类型下拉 var initBusiTypeSel = function( ...

  4. Chosen:Select 选择框的华丽变身

    HTML Form 表单里的各种组件,例如文本输入框,textarea,按钮等,都可以通过CSS或其它技术进行美化,让它们看起来很漂亮了,唯独下拉列表选项框(select box),不管你怎么做,它摆 ...

  5. Java开发笔记(一百二十二)AWT选择框

    前面介绍了两种文本输入框的用法,不过实际应用很少需要用户亲自文字,而是在界面上列出几个选项,让用户勾勾点点完成选择,这样既方便也不容易弄错.依据选择的唯一性,可将选项控件分为两类:一类是在方框中打勾的 ...

  6. Java开发笔记(一百三十)Swing的选择框

    不管是AWT还是Swing,都把选择框分成两类:复选框和单选按钮,这两类控件无论是外观上还是功能上均有显著差异.例如,在外观方面,复选框是在方框内打勾,而单选按钮是在圆圈内画圆点:在功能方面,复选框允 ...

  7. css自定义 range radio select的样式滑轮,按钮,选择框

    写在前面: 之前踩坑css的时候,遇到滑轮,按钮,选择框这类型的东西,为了页面效果,总是需要自定义他们的样式,而不使用他们的默认样式.当时写的时候,我也是蛮头疼的,弄了个demo,链接在下面.对此做个 ...

  8. java、easyui-combotree树形下拉选择框

    最近一直在研究这个树形的下拉选择框,感觉非常的有用,现在整理下来供大家使用: 首先数据库的表架构设计和三级菜单联动的表结构是一样,(父子关系) 1.下面我们用hibernate建一下对应的额实体类: ...

  9. Notes: select选择框

    HTML选择框通过select标签创建,该元素是HTMLSelectElement的实例,拥有以下属性和方法: selectedIndex:选中项的索引 options:选择框的所有选项 add:向选 ...

随机推荐

  1. activity入门2

    1.如何获取其他应用的包名和类名? 点击和查看logcat第一条信息2.第二步Intent intent = new Intent();intent.setClassName("com.an ...

  2. 04-IOSCore - User Defaults、Archive、存储总结

    一. User Defaults 1. 是什么? 是一个特殊的plist文件 2. 干什么? 用于保存应用的配置信息 3. 存什么信息? 信息:欢迎界面有没有被打开过 目的:欢迎界面只显示一次 信息: ...

  3. 演练5-3:Contoso大学校园管理系统3

    在前面的教程中,我们使用了一个简单的数据模型,包括三个数据实体.在这个教程汇中,我们将添加更多的实体和关系,按照特定的格式和验证规则等自定义数据模型. Contoso大学校园管理系统的数据模型如下. ...

  4. Cubieboard4卡片式电脑

    Cubieboard4 also named CC-A80, is a open source mini PC or single board computer which has ultra-pow ...

  5. 【leetcode】Single Number II

    int singleNumber(int A[], int n) { int once = 0; int twice = 0; int three = 0; for (int i = 0; i < ...

  6. C++多继承的好处是增加了弹性和灵活性,Delphi类强迫单继承TObject是为了提供许多强大功能

    要说灵活性,是C++更强.我自己开发已经好几次碰到需要多继承的情况了. 但是Delphi强迫继承TObject,虽然是单继承,但是提供了相当多的强力功能.要说强大,那还是Delphi当仁不让. 摘自& ...

  7. php的var关键字

    public和var的作用差不多 因为 var定义的变量如果没有加protected 或 private则默认为public php4 中一般是用 varphp5 中就一般是用 public了 现在基 ...

  8. Swift - 委托(delegate)的介绍,及使用样例

    1,委托的说明 委托(delegate)是Cocoa的一个术语,表示将一个对象的部分功能转交给另一个对象. 比如对象A希望对象B知道将要发生或已经发生某件事情,对象A可以把对象B的引用存为一个实例变量 ...

  9. PHP学习之-1.1 PHP 可以做什么?

    PHP 可以做什么? 为什么要学习PHP,"我可以用javascript来实现程序的编写."但是javascript的能力是有限的,javascript通常运行在浏览器(客户端), ...

  10. shell程序设计(转)

    1.shell脚本的基本概念: (1)Shell执行的是称为shell程序,这些程序通常被称为脚本. (2)Shell是一个用户和系统间接口的程序,它允许用户向操作系统输入需要执行的命令. (3)sh ...