Swift - 选择框(UIPickerView)的用法
生活随笔
收集整理的這篇文章主要介紹了
Swift - 选择框(UIPickerView)的用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1,選擇框可以讓用戶以滑動的方式選擇值。示例如下:
2,調整選擇框的尺寸
UIPickerView用frame和center兩個屬性設置整個選擇框的大小和位置。
如果要調整內部列的寬度,需要實現UIPickerViewDelegate協議類中pickerView:widthForComponent方法設置
如果要調整內部行高,則需要實習上述協議類中pickerView:rowHeightForComponent方法設置
3,將圖片作為選擇框選項
選擇框選項的內容,除了可以使字符串類型的,還可以是任意UIView類型的元素。比如我們將選項內容設置為圖片:
4,檢測響應選項的選擇狀態
| 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) } |
轉載于:https://www.cnblogs.com/Free-Thinker/p/4838171.html
總結
以上是生活随笔為你收集整理的Swift - 选择框(UIPickerView)的用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [MySQL]命令行工具和基本操作
- 下一篇: Swift - 添加、修改、删除通讯录联