#実現したいこと
datepickerとUIToolbarを組み合わせて、生年月日を選択できるようにしたいです。
textfieldをタップしたら、下からtoolbarを表示し年月日を選択でき、完了ボタンを押したら閉じるような形にしたいです。
#現状
UITextfieldとUIDatePickerを連携させる
上記の記事を参考に以下のようなコードを書きました。
import UIKit class firstViewController: UIViewController, UITextFieldDelegate, UIToolbarDelegate { var toolBar:UIToolbar! @IBOutlet weak var birthday: UITextField! override func viewDidLoad() { super.viewDidLoad() birthday.delegate = self //datepicker上のtoolbarの「完了」ボタン toolBar = UIToolbar() toolBar.sizeToFit() let toolBarBtn = UIBarButtonItem(title: "完了", style: .plain, target: self, action: #selector(firstViewController.doneBtn)) toolBar.items = [toolBarBtn] birthday.inputAccessoryView = toolBar } //textfieldをタップされたらdatepickerを表示する @IBAction func textFieldEditing(sender: UITextField) { let datePickerView:UIDatePicker = UIDatePicker() datePickerView.datePickerMode = UIDatePickerMode.date sender.inputView = datePickerView datePickerView.addTarget(self, action: #selector(firstViewController.datePickerValueChanged), for: UIControlEvents.valueChanged) } //datepickerが選択されたらtextfieldに表示 @objc func datePickerValueChanged(sender:UIDatePicker) { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy/MM/dd"; birthday.text = dateFormatter.string(from: sender.date) } //toolbarのdoneボタン @objc func doneBtn(){ birthday.resignFirstResponder() } }
ビルドしてシュミレーターでテストすると、toolbarの一部が表示されるものの完了ボタンを押すと下記のようなエラーが発生してしまいます。
2018-01-10 18:11:45.293547+0900 squiz-test[48414:4209827] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[squiz_test.firstViewController birthdayEditing:]: unrecognized selector sent to instance 0x7f96cc429b40' *** First throw call stack: ( 0 CoreFoundation 0x000000010634612b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x00000001059daf41 objc_exception_throw + 48 2 CoreFoundation 0x00000001063c7024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 UIKit 0x00000001073a9f51 -[UIResponder doesNotRecognizeSelector:] + 295 4 CoreFoundation 0x00000001062c8f78 ___forwarding___ + 1432 5 CoreFoundation 0x00000001062c8958 _CF_forwarding_prep_0 + 120 6 UIKit 0x0000000107177972 -[UIApplication sendAction:to:from:forEvent:] + 83 7 UIKit 0x00000001072f6c3c -[UIControl sendAction:to:forEvent:] + 67 8 UIKit 0x00000001072f6f59 -[UIControl _sendActionsForEvents:withEvent:] + 450 9 UIKit 0x0000000107e9f407 -[UITextField _resignFirstResponder] + 155 10 UIKit 0x00000001073a9939 -[UIResponder _finishResignFirstResponder] + 286 11 UIKit 0x0000000107e9f026 -[UITextField _finishResignFirstResponder] + 48 12 UIKit 0x00000001073a9a6f -[UIResponder resignFirstResponder] + 275 13 UIKit 0x0000000107e9eef6 -[UITextField resignFirstResponder] + 135 14 squiz-test 0x00000001037559dd _T010squiz_test19firstViewControllerC7doneBtnyyF + 141 15 squiz-test 0x0000000103755a24 _T010squiz_test19firstViewControllerC7doneBtnyyFTo + 36 16 UIKit 0x0000000107177972 -[UIApplication sendAction:to:from:forEvent:] + 83 17 UIKit 0x0000000107b89290 __45-[_UIButtonBarTargetAction _invoke:forEvent:]_block_invoke + 154 18 UIKit 0x0000000107b891c1 -[_UIButtonBarTargetAction _invoke:forEvent:] + 181 19 UIKit 0x0000000107177972 -[UIApplication sendAction:to:from:forEvent:] + 83 20 UIKit 0x00000001072f6c3c -[UIControl sendAction:to:forEvent:] + 67 21 UIKit 0x00000001072f6f59 -[UIControl _sendActionsForEvents:withEvent:] + 450 22 UIKit 0x00000001072f5e86 -[UIControl touchesEnded:withEvent:] + 618 23 UIKit 0x00000001071ed807 -[UIWindow _sendTouchesForEvent:] + 2807 24 UIKit 0x00000001071eef2a -[UIWindow sendEvent:] + 4124 25 UIKit 0x0000000107192365 -[UIApplication sendEvent:] + 352 26 UIKit 0x0000000107adea1d __dispatchPreprocessedEventFromEventQueue + 2809 27 UIKit 0x0000000107ae1672 __handleEventQueueInternal + 5957 28 CoreFoundation 0x00000001062e9101 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 29 CoreFoundation 0x0000000106388f71 __CFRunLoopDoSource0 + 81 30 CoreFoundation 0x00000001062cda19 __CFRunLoopDoSources0 + 185 31 CoreFoundation 0x00000001062ccfff __CFRunLoopRun + 1279 32 CoreFoundation 0x00000001062cc889 CFRunLoopRunSpecific + 409 33 GraphicsServices 0x000000010c6679c6 GSEventRunModal + 62 34 UIKit 0x00000001071765d6 UIApplicationMain + 159 35 squiz-test 0x0000000103739767 main + 55 36 libdyld.dylib 0x0000000109fccd81 start + 1 37 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException
unrecognized selector sent to instance 0x7f96cc429b40'
この部分がエラーの主な原因ではないかと考えています。
上記のコードですとどこがおかしいでしょうか。
よろしくお願いいたします。
@objc func doneBtn(){ birthday.resignFirstResponder() } ここのbirthdayはどこで定義されていますか?インスタンス変数として定義されてないので、どこから参照しているものかわからないのですが。後クラス名は必ず大文字にしたほうがいいかと思います
申し訳ございません。編集いたしました。
修正ありがとうございます。birthdayがうまく接続されてないということはないでしょうか?
firstViewControllerの birthdayEditing: が無くて落ちています。これは何でしょうか?どこから呼び出しているのでしょうか?
@IBAction func textFieldEditing(sender: UITextField) {をbirthdayEditingに変え接続し直したところエラーが発生しなくなりました…!ありがとうございました。
回答1件
あなたの回答
tips
プレビュー