import UIKit

class ViewController: UIViewController , UIPickerViewDelegate,UIPickerViewDataSource{

var pickerView: UIPickerView!

override func viewDidLoad() {

super.viewDidLoad()

pickerView = UIPickerView()

pickerView.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 300)

pickerView.delegate = self

pickerView.dataSource = 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)

// 建立一个按钮, 触摸按钮时获得选择框被选择的索引

let button = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 30))

button.center = self.view.center

button.backgroundColor = UIColor.red

button.setTitle("按钮", for: .normal)

self.view.addSubview(button)

button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)

}

// 设置选择框的列数, 继承于 UIPickerViewDataSource 协议

func numberOfComponents(in pickerView: UIPickerView) -> Int {

return 3

}

// 设置选择框的行数, 继承于 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)

}

// 设置列的宽度, 继承于 UIPickerViewDelegate 协议

//    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {

//        return self.view.frame.size.width/3

//    }

// 设置行的高度, 继承于 UIPickerViewDelegate 协议

func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {

return 50

}

// 可以将图片作为选择框内容

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

// 检测响应选项的选择状态

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

print("第 \(component) 列, 第 \(row) 行")

}

// 触摸按钮时,获得被选中的索引

func buttonAction(sender: UIButton) {

let message = String(pickerView.selectedRow(inComponent: 0)) + "-"

+ String(pickerView!.selectedRow(inComponent: 1)) + "-"

+ String(pickerView.selectedRow(inComponent: 2))

print(message)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

swift 实践- 12 -- UIPickerView的更多相关文章

  1. 【原创 Hadoop&Spark 动手实践 12】Spark MLLib 基础、应用与信用卡欺诈检测系统动手实践

    [原创 Hadoop&Spark 动手实践 12]Spark MLLib 基础.应用与信用卡欺诈检测系统动手实践

  2. Swift进阶 - 12个技巧

    听说你已经学习Swift几个月了,有没有想更进一步成为Swift高手的想法?我这里有11招秘技,各位施主且听我慢慢道来,结个善缘. 1. 扩展(Extension) 任务: 求数字的平方. // 菜鸟 ...

  3. Socket编程实践(12) --UDP编程基础

    UDP特点 无连接,面向数据报(基于消息,不会粘包)的传输数据服务; 不可靠(可能会丢包, 乱序, 反复), 但因此普通情况下UDP更加高效; UDP客户/服务器模型 UDP-API使用 #inclu ...

  4. 《高级软件测试》web测试实践--12月31日记录

    今日的任务进度如上图所示.我们对华科软件学院和计算机学院的网站进行了对比分析,分析的角度包括基本功能分析.前端性能分析.用户调研等.在这里我们简单总结下我们得到的评测结果. 基本功能分析:计算机学院和 ...

  5. 《高级软件测试》web测试实践--12月30日记录

    考完数学,我们正式开始web测试实践的作业,今天,我们主要进行了方案的选择和人员的分工.任务计划和安排如上图所示. 任务进展:完成题目选择和人员分工: 遇到问题:暂无: 下一步任务:完成软件评测.用户 ...

  6. Swift基础之UIPickerView和小animate的使用

    写一个简单的UIPickerView的使用Demo,比较简单,其中和一个小动画的结合使用 UIPickerView的使用基本上跟OC语言中的一样,就是写法的样式问题,想必开发过OC的应该不需要多讲了, ...

  7. Linux IPC实践(12) --System V信号量(2)

    实践1:信号量实现进程互斥 父子进程执行流程如下: 父进程 子进程 P P O(print) X(print) sleep sleep O(print) X(print) V V sleep slee ...

  8. swift 实践- 08 -- UISegmentedControl

    import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...

  9. swift 学习- 12 -- 方法

    // 方法 是与某些特定类型相关的函数.  类, 结构体,枚举 都可以定义实例方法, 实例方法为给类型的实例封装了具体的任务与功能.  类, 结构体, 枚举 也可以定义类型方法,  类型方法与类型本身 ...

随机推荐

  1. luogu 2480 古代猪文 数论合集(CRT+Lucas+qpow+逆元)

    一句话题意:G 的 sigma d|n  C(n d) 次幂  mod 999911659 (我好辣鸡呀还是不会mathjax) 分析: 1.利用欧拉定理简化模运算 ,将上方幂设为x,则x=原式mod ...

  2. Spring Boot 起步

    ……………………………………………………………………………………………………………… [应用配置]application.yml [port][context-path][datasource][jp ...

  3. 【codeforces 870F】Paths

    Description You are given a positive integer n. Let's build a graph on vertices 1, 2, ..., n in such ...

  4. 剑指offer: 数组中的逆序对

    1. 最简单的思路,对每个值,遍历与其逆序的数组对:但时间复杂度太高: 2. 归并排序的思路: 先将数组分隔成子数组,先统计出子数组内的逆序对的数目,然后统计两个相邻子数组之间的逆序对的数目: int ...

  5. es6写法

    我们在日常开发中,如果我们使用es5则可以直接在浏览器里面写JavaScript脚本.一点问题也没有. 但是在写es6语法的JavaScript代码的时候,我们就需要引入babel翻译器了. 例如: ...

  6. C语言编程程序的内存如何布局

    重点关注以下内容: C语言程序在内存中各个段的组成 C语言程序连接过程中的特性和常见错误 C语言程序的运行方式 一:C语言程序的存储区域 由C语言代码(文本文件)形成可执行程序(二进制文件),需要经过 ...

  7. Hive通过mysql元数据表删除分区

    1 创建表 hive命令行操作 CREATE TABLE IF NOT EXISTS emp( name STRING, salary FLOAT, subordinates ARRAY<STR ...

  8. Oracle 11g R2 for Win10(64位)的安装步骤

    下载 官网下载地址: win64_11gR2_database_1of2.zip win64_11gR2_database_2of2.zip 将两个压缩包解压到同一个目录下,即"databa ...

  9. aplication.properties配置

    1.设置使用的properties文件 spring.profiles.active=dev 设置激活使用哪个properties一般设置两个,一个是开发环境的,一个是本地的测试环境 可设置默认使用开 ...

  10. SQL Server查询数据库所有存储过程、触发器、索引信息SQL分享

    1. 查询所有存储过程 1 select Pr_Name as [存储过程], [参数]=stuff((select ','+[Parameter] 2 from ( 3 select Pr.Name ...