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. 使用Jackson时转换JSON时,日期格式设置

    在我们使用jackjson时时间默认输出如下: 输出是一串时间戳,不符合我们的要求,所以想到jackjson对时间的处理有他默认的格式,然后网上搜集各种资料,得出一下方式可以解决 取消jackjson ...

  2. 树链剖分模板(洛谷P3384)

    洛谷P3384 #include <bits/stdc++.h> #define DBG(x) cerr << #x << " = " < ...

  3. json对象转数组

    <script type="text/javascript"> var object = {"a":1,"b":2," ...

  4. 使用Docker部署javaWeb应用

    1. 安装Dcoker http://www.cnblogs.com/zhangqian27/p/9089815.html 2. 查看镜像 $ docker images 3. 搜索镜像 $ dock ...

  5. python 08

    函数 函数定义: 你可以定义一个由自己想要功能的函数,以下是简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号(). 任何传入参数和自变量必须放在圆括号中间.圆括号之间可以用于 ...

  6. 🍓 react,jroll滑动删除 🍓

    import React, { Component } from 'react'; import '../src/css/reset.css'; import '../src/css/delete.c ...

  7. 《一头扎进SpringMvc视频教程》

    第二章 SpringMvc控制器 第三章 Rest风格的资源URL 第四章 SpringMvc上传文件

  8. DeepLearning.ai-Week2-Keras tutorial-the Happy House

    1 - Import Packages import numpy as np from keras import layers from keras.layers import Input, Dens ...

  9. 修复服务器上出现ImportError: cannot import name main的问题

    在服务器上成功升级pip2之后再运行pip2命令出现如下报错信息 Traceback (most recent call last): File "/usr/bin/pip2.7" ...

  10. Linux查看本机IP:curl cip.cc

    curl http://members.3322.org/dyndns/getip curl ip.6655.com/ip.aspx curl ifconfig.me curl icanhazip.c ...