Xcode version11.5
Swift5
下のstoryboadのようにTextFieldをタップするとpickerがしたから出てきて、選択が完了した後に閉じるボタンを押すとpickerを閉じるようにしたいのですが、以下のようなエラーを吐いてしまいます。
Thread 1: Exception: "-[Picker.ViewController infoTextField:]: unrecognized selector sent to instance 0x102f0a800"
どうか解決方法を教えていただきたいです。
swift
1import UIKit 2 3class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { 4 func numberOfComponents(in pickerView: UIPickerView) -> Int { 5 return 3 6 } 7 8 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 9 switch component { 10 case 0: 11 return hourList.count 12 case 1: 13 return minuteList.count 14 case 2: 15 return categoryList.count 16 default: 17 return 0 18 } 19 } 20 var tmpHour = "--" 21 var tmpMinute = "--" 22 var tmpCategory = "--" 23 24 let hourList = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"] 25 var minuteList = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09"] 26 let categoryList = ["食事", "勉強", "ハミガキ", "就寝", "起床", "薬", "その他"] 27 28 override func viewDidLoad() { 29 super.viewDidLoad() 30 // Do any additional setup after loading the view. 31 for i in 10..<60 { 32 minuteList.append(String(i)) 33 // print(i) 34 } 35 myPicker = UIPickerView() 36 myPicker.delegate = self 37 myPicker.dataSource = self 38 myPicker.selectRow(0, inComponent: 0, animated: false) 39 myPicker.selectRow(1, inComponent: 1, animated: false) 40 myPicker.selectRow(2, inComponent: 2, animated: false) 41 //閉じるツールバーの生成 42 let kbToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 40)) 43 kbToolBar.barStyle = UIBarStyle.default // スタイルを設定 44 kbToolBar.sizeToFit() // 画面幅に合わせてサイズを変更 45 46 let spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil) 47 // 閉じるボタン 48 let commitButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector(ViewController.commitButtonTapped)) 49 kbToolBar.items = [spacer, commitButton] 50 51 infoTextField.inputView = myPicker 52 infoTextField.inputAccessoryView = kbToolBar 53 } 54 @objc func commitButtonTapped (){ 55 self.view.endEditing(true) 56 self.resignFirstResponder() 57 } 58 59 func getCategory() -> String { 60 return self.tmpCategory 61 } 62 @IBOutlet weak var infoTextField: UITextField! 63 64 private var myPicker: UIPickerView! 65 func pickerView(_ pickerView: UIPickerView, 66 titleForRow row: Int, 67 forComponent component: Int) -> String? { 68 switch component { 69 case 0: 70 return hourList[row] 71 case 1: 72 return minuteList[row] 73 case 2: 74 return categoryList[row] 75 default: 76 return "error" 77 } 78 } 79 80 func pickerView(_ pickerView: UIPickerView, 81 didSelectRow row: Int, 82 inComponent component: Int) { 83 switch component { 84 case 0: 85 tmpHour = hourList[row] 86 infoTextField.text = tmpHour + ": " + tmpMinute + " , " + tmpCategory 87 case 1: 88 tmpMinute = minuteList[row] 89 infoTextField.text = tmpHour + ": " + tmpMinute + " , " + tmpCategory 90 case 2: 91 tmpCategory = categoryList[row] 92 infoTextField.text = tmpHour + ": " + tmpMinute + " , " + tmpCategory 93 default: 94 break 95 } 96 } 97 98} 99 100
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/13 05:59