目次

[Swift5]同じビュー内に違う値を持つPickerViewを2つ表示する方法

2020年1月2日

久々の更新、最近はiphoneのアプリ開発を勉強しています。
今日は、同一ビュー内に異なる値のPickerViewを表示したかったが
あまり、参考になるサイトが少なかったのでわかったことをメモしておく。
*あくまでメモなので参考程度に

まずは結果から

まずは、動作結果を動画にしてみた。

PickerViewの使い方は、以下のサイトがわかりやすかった。

ソースコード全体

今回作成した全体のソースを表示する。
今回は、ゲームの参加人数と制限時間をPickerViewにしてみた。

[swift]
import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet weak var joinnum_picker: UIPickerView!
@IBOutlet weak var limitedtime_picker: UIPickerView!

//参加人数
let JoinNum = ["4","5","6","7","8","9","10"]
//制限時間
let limitTime = ["3:00","5:00","7:00","10:00"]

//表示するリストの数
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

//表示する配列の数
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 1{
return JoinNum.count
} else if pickerView.tag == 2{
return limitTime.count
} else {
return JoinNum.count
}
}

//データを返すメソッド
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

if pickerView.tag == 1{
return JoinNum[row]
} else if pickerView.tag == 2{
return limitTime[row]
} else {
return JoinNum[row]
}
}

//データ選択時の呼び出しメソッド
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//pickerviewの設定、tagで表示内容を分ける
joinnum_picker.delegate = self
joinnum_picker.dataSource = self
joinnum_picker.tag = 1
limitedtime_picker.delegate = self
limitedtime_picker.dataSource = self
limitedtime_picker.tag = 2
}

}

[/swift]

ポイント

まず、viewDidLoadないでpickerviewの定義をする時に
tagを設定する、これが複数使用する時のポイントのようだ。

[swift]
joinnum_picker.delegate = self
joinnum_picker.dataSource = self
joinnum_picker.tag = 1
limitedtime_picker.delegate = self
limitedtime_picker.dataSource = self
limitedtime_picker.tag = 2
[/swift]

後は、複数定義をしたい所をtagごとに処理わけすれば問題なさそう

[swift]
//表示する配列の数
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 1{
return JoinNum.count
} else if pickerView.tag == 2{
return limitTime.count
} else {
return JoinNum.count
}
}

//データを返すメソッド
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

if pickerView.tag == 1{
return JoinNum[row]
} else if pickerView.tag == 2{
return limitTime[row]
} else {
return JoinNum[row]
}
}
[/swift]

さいごに

とりあえず、これで出来るがもっと最適解があるかはわからない



Warning: Undefined array key 0 in /home/sonitan/etolog.jp/public_html/wp-content/themes/luxeritas/inc/blogcard-func.php on line 580

Fatal error: Uncaught TypeError: json_decode(): Argument #1 ($json) must be of type string, array given in /home/sonitan/etolog.jp/public_html/wp-content/themes/luxeritas/inc/blogcard-func.php:884 Stack trace: #0 /home/sonitan/etolog.jp/public_html/wp-content/themes/luxeritas/inc/blogcard-func.php(884): json_decode(Array, true) #1 /home/sonitan/etolog.jp/public_html/wp-content/themes/luxeritas/inc/blogcard-func.php(748): THK_Blogcard->thk_get_screenshot('https://i-app-t...') #2 /home/sonitan/etolog.jp/public_html/wp-content/themes/luxeritas/inc/blogcard-func.php(318): THK_Blogcard->thk_create_blogcard('https://i-app-t...', '2a69f9491fbc51d...') #3 /home/sonitan/etolog.jp/public_html/wp-content/themes/luxeritas/inc/blogcard-func.php(142): THK_Blogcard->thk_create_blogcard_shutdown('<p>\xE4\xB9\x85\xE3\x80\x85\xE3\x81\xAE\xE6\x9B\xB4...') #4 /home/sonitan/etolog.jp/public_html/wp-includes/class-wp-hook.php(341): THK_Blogcard->{closure:THK_Blogcard::thk_replace_blogcard():141}('') #5 /home/sonitan/etolog.jp/public_html/wp-includes/class-wp-hook.php(365): WP_Hook->apply_filters(1, Array) #6 /home/sonitan/etolog.jp/public_html/wp-includes/plugin.php(522): WP_Hook->do_action(Array) #7 /home/sonitan/etolog.jp/public_html/wp-includes/load.php(1308): do_action('shutdown') #8 [internal function]: shutdown_action_hook() #9 {main} thrown in /home/sonitan/etolog.jp/public_html/wp-content/themes/luxeritas/inc/blogcard-func.php on line 884