前提・実現したいこと
こんにちは。Swiftでアプリケーションを作成しています。
画像上のボタンを押すと、下からピッカーが出てきて、ピッカーを選択すると、ラベルに対して選択した文字列を表示させる機能を実装したいです。
しかし、ボタンからピッカーを出す方法がわかりません。
ピッカーはテキストフィールドとしか連携できないのでしょうか?
以下は参考にした記事です。
こちらの回答をそのまま試したところ、テキストフィールドからの起動は実装できました。
記事;https://teratail.com/questions/53556
単純に上記記事のコードのテキストフィールとボタンとを置換しようと思ったのですが、
textField.inputView = vi textField.inputAccessoryView = toolBar
の部分をどうしていいのかがわかりません。
一応以下のように変更して見たのですが動作してくれません…
該当のソースコード
Swift
1 2 3 func PickerPush(){ 4 pickerView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: pickerView.bounds.size.height) 5 // Connect data: 6 pickerView.delegate = self 7 pickerView.dataSource = self 8 let vi = UIView(frame: pickerView.bounds) 9 vi.backgroundColor = UIColor.white 10 FrontView.addSubview(pickerView) 11 let toolBar = UIToolbar() 12 toolBar.barStyle = UIBarStyle.default 13 toolBar.isTranslucent = true 14 toolBar.tintColor = UIColor.black 15 let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(StateViewController.donePressed)) 16 let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(StateViewController.cancelPressed)) 17 let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) 18 toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false) 19 toolBar.isUserInteractionEnabled = true 20 toolBar.sizeToFit() 21 vi.addSubview(toolBar) 22 print("push") 23 } 24 @IBAction func HeadButton(_ sender: Any) { 25 PickerPush() 26 }
見当違いのことを書いているかと思います。大変申し訳ありません。
ご存知の方がおられれば、ボタンを押した時にピッカーがしたから出る方法を教えていただけないでしょうか。
よろしくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/03 06:47