前提・実現したいこと
UIPickerViewでTimerをセットしたいです。
該当のソースコード
swift
1import UIKit 2 3class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { 4 5 @IBOutlet weak var testPickerView: UIPickerView! 6// @IBOutlet weak var testLabel: UILabel! 7 8 var timer:Timer! 9 var count:Int = 0 10 11 //時分秒のデータ 12 let dataList = [[Int](0...24), [Int](0...60), [Int](0...60)] 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 // Do any additional setup after loading the view, typically from a nib. 17 18 //「時間」のラベルを追加 19 let hStr = UILabel() 20 hStr.text = "時間" 21 hStr.sizeToFit() 22 hStr.frame = CGRect(x:testPickerView.bounds.width/4 - hStr.bounds.width/2, 23 y:testPickerView.bounds.height/2 - (hStr.bounds.height/2), 24 width:hStr.bounds.width, height:hStr.bounds.height) 25 testPickerView.addSubview(hStr) 26 27 //「分」のラベルを追加 28 let mStr = UILabel() 29 mStr.text = "分" 30 mStr.sizeToFit() 31 mStr.frame = CGRect(x:testPickerView.bounds.width/2 - mStr.bounds.width/2, 32 y:testPickerView.bounds.height/2 - (mStr.bounds.height/2), 33 width:mStr.bounds.width, height:mStr.bounds.height) 34 testPickerView.addSubview(mStr) 35 36 37 //「秒」のラベルを追加 38 let sStr = UILabel() 39 sStr.text = "秒" 40 sStr.sizeToFit() 41 sStr.frame = CGRect(x:testPickerView.bounds.width*3/4 - sStr.bounds.width/2, 42 y:testPickerView.bounds.height/2 - (sStr.bounds.height/2), 43 width:sStr.bounds.width, height:sStr.bounds.height) 44 testPickerView.addSubview(sStr) 45 46 } 47 48 override func didReceiveMemoryWarning() { 49 super.didReceiveMemoryWarning() 50 // Dispose of any resources that can be recreated. 51 } 52 53 //コンポーネントの個数を返すメソッド 54 func numberOfComponents(in pickerView: UIPickerView) -> Int { 55 return dataList.count 56 } 57 58 59 //コンポーネントに含まれるデータの個数を返すメソッド 60 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 61 return dataList[component].count 62 } 63 64 65 //サイズを返すメソッド 66 func pickerView(_ pickerView: UIPickerView, widthForComponent component:Int) -> CGFloat { 67 return testPickerView.bounds.width * 1/4 68 69 } 70 71 72 //データを返すメソッド 73 func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { 74 75 let pickerLabel = UILabel() 76 pickerLabel.textAlignment = NSTextAlignment.left 77 pickerLabel.text = String(dataList[component][row]) 78 pickerLabel.backgroundColor = UIColor.red 79 80 return pickerLabel 81 82 } 83 84} 85
###困っている点
実行するとUIPickerViewの選択肢が表示されません
補足情報(FW/ツールのバージョンなど)
swift4.1
ios11.4
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/26 20:37