![見出し画像](https://assets.st-note.com/production/uploads/images/100554969/rectangle_large_type_2_0c31fd3fd7aaa6466107463705c2911c.png?width=1200)
【徒然iOS】気ままにUIKit47〜Picker Viewの使い方。自前の選択肢のホイールを回す。〜
概要
このマガジンは四十を過ぎたおっさんが、
を参考にStoryboardでiOSアプリを完全に趣味で楽しんでいるだけな記事を気ままに上げてます。
今回
をレッツゴ🕺
前準備
念の為、
バックアップ
新しいクラス
ビューコントローラの追加
イニシャルビューの変更
をいつも通りやってから本題へ💃
![](https://assets.st-note.com/img/1679112868103-TOWYdi5q9p.png?width=1200)
本題
ピッカービューとは、
ホイールを回してデータを選択する部品
・似たような部品に「Date Picker」(以下、デートピッカー)がある。
・デートピッカーは日時の選択に特化した部品で、ピッカービューは選択肢のデータを自分で用意するところが異なる。
では、実際に
⒈ピッカービューを配置
![](https://assets.st-note.com/img/1679112999706-vY3XWe2Uvm.png?width=1200)
![](https://assets.st-note.com/img/1679113012263-DS6Igr56gt.png?width=1200)
⒉dataSouceを選択してビュー本体と接続
![](https://assets.st-note.com/img/1679113078443-dWQQP4xiZ3.png?width=1200)
⒊delegateを選択する
![](https://assets.st-note.com/img/1679114902301-WswbCSrejb.png?width=1200)
![](https://assets.st-note.com/img/1679114925138-W0m1Og72wi.png?width=1200)
ここでポイント:
ここでdelegateし忘れると、コードを組んでもデータが出てこないから、忘れないようにね〜〜〜〜🕺
(一回シクって、30分くらい出てこないので悩んだ💦)
⒋ラベルを配置してアウトレット接続
![](https://assets.st-note.com/img/1679113166629-9RVzbHPUsh.png?width=1200)
![](https://assets.st-note.com/img/1679113179518-ZfycFZopiR.png?width=1200)
![](https://assets.st-note.com/img/1679113192105-Eajy6MnUS6.png?width=1200)
![](https://assets.st-note.com/img/1679115019036-fBfRF7xvYH.png?width=1200)
![](https://assets.st-note.com/img/1679115060998-rzTo7OW7IU.png?width=1200)
⒌コードを実装する
//
// ViewController.swift
//
import UIKit
class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
@IBOutlet weak var testLabel: UILabel!
let dataList = [["牛丼","豚丼","すき焼き丼","鮭定食"],["並盛り","大盛り","特盛り"],["一人前","二人前","三人前"]]
//最初からあるメソッド
override func viewDidLoad() {
super.viewDidLoad()
}
//コンポーネントの個数を返すメソッド
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return dataList.count
}
//コンポーネントに含まれるデータの個数を返すメソッド
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dataList[component].count
}
//データを返すメソッド
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dataList[component][row]
}
//データ選択時の呼び出しメソッド
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//コンポーネントごとに現在選択されているデータを取得する。
let data1 = self.pickerView(pickerView, titleForRow: pickerView.selectedRowInComponent(0), forComponent: 0)
let data2 = self.pickerView(pickerView, titleForRow: pickerView.selectedRowInComponent(1), forComponent: 1)
let data3 = self.pickerView(pickerView, titleForRow: pickerView.selectedRowInComponent(2), forComponent: 2)
testLabel.text = "選択 \(data1!) \(data2!) \(data3!)"
}
}
を実装しろってことなので〜〜〜〜組み込むと、
![](https://assets.st-note.com/img/1679115160974-Vp7o04IVOe.png?width=1200)
FixとJumptoDefinitionを駆使しながら直そう🕺
⒍シミュレータ実行
![](https://assets.st-note.com/img/1679115407907-6pfjo562nD.png?width=1200)
⒎設定項目を確認
![](https://assets.st-note.com/img/1679115537682-PkxxwquIZX.png)
ってことだけ確認🕺
サイト記事については以上。
⒏ブラッシュアップ
さて今回もVerticalStackViewなどを使って修正〜〜〜
![](https://assets.st-note.com/img/1679115842699-8akL2rPgky.png?width=1200)
![](https://assets.st-note.com/img/1679115868403-oZTUOGX3s1.png?width=1200)
![](https://assets.st-note.com/img/1679115886469-QZjSYMS21t.png?width=1200)
![](https://assets.st-note.com/img/1679115904823-wVU6yYngKD.png?width=1200)
![](https://assets.st-note.com/img/1679115925887-ItfegVKex4.png?width=1200)
![](https://assets.st-note.com/img/1679115974151-einT48CAim.png?width=1200)
![](https://assets.st-note.com/img/1679116035609-FVUJQnN9w6.png?width=1200)
![](https://assets.st-note.com/img/1679116080121-y1cCQwNtad.png?width=1200)
今回のコード(まとめ)
class PickerStandardViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
@IBOutlet weak var myPickerLabel: UILabel!
let dataList = [["牛丼","豚丼","すき焼き丼","鮭定食"],["並盛り","大盛り","特盛り"],["一人前","二人前","三人前"]]
//最初からあるメソッド
override func viewDidLoad() {
super.viewDidLoad()
}
//コンポーネントの個数を返すメソッド
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return dataList.count
}
//コンポーネントに含まれるデータの個数を返すメソッド
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dataList[component].count
}
//データを返すメソッド
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dataList[component][row]
}
//データ選択時の呼び出しメソッド
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//コンポーネントごとに現在選択されているデータを取得する。
let data1 = self.pickerView(pickerView, titleForRow: pickerView.selectedRow(inComponent: 0), forComponent: 0)
let data2 = self.pickerView(pickerView, titleForRow: pickerView.selectedRow(inComponent: 1), forComponent: 1)
let data3 = self.pickerView(pickerView, titleForRow: pickerView.selectedRow(inComponent: 2), forComponent: 2)
myPickerLabel.text = "選択 \(data1!) \(data2!) \(data3!)"
}
}
Apple公式
さて、次回は
をレッツゴする〜〜〜〜🕺