##やりたいこと
pickerviewでdoneボタンとcancelボタンを追加したいのですが下記のエラーが出てしまい、困っています。
error
1Value of type '(Any) -> ()' has no member 'done' 2Value of type '(Any) -> ()' has no member 'cancel'
#該当部分
let doneItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(addEvent.done)) let cancelItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(addEvent.cancel))
#コード全文
import UIKit import RealmSwift class addEvent: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { @IBOutlet weak var textField: UITextField! var pickerView: UIPickerView = UIPickerView() let list = ["", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] @IBOutlet weak var selectLabel: UILabel! @IBOutlet weak var eventTF: UITextField! @IBOutlet weak var datePicker: UIDatePicker! override func viewDidLoad() { super.viewDidLoad() pickerView.delegate = self pickerView.dataSource = self print(Realm.Configuration.defaultConfiguration.fileURL!) let toolbar = UIToolbar(frame: CGRectMake(0, 0, 0, 35)) let doneItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(addEvent.done)) let cancelItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(addEvent.cancel)) self.textField.inputView = pickerView self.textField.inputAccessoryView = toolbar datePicker.datePickerMode = UIDatePicker.Mode.date datePicker.timeZone = NSTimeZone.local datePicker.locale = NSLocale(localeIdentifier: "ja_JP") as Locale datePicker.addTarget(self, action: #selector(picker(_:)), for: .valueChanged) view.addSubview(selectLabel) } func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return list.count } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return list[row] } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { self.textField.text = list[row] } func cancel() { self.textField.text = "" self.textField.endEditing(true) } func done() { self.textField.endEditing(true) } func CGRectMake(_ x: CGFloat, _ y:CGFloat, _ width: CGFloat, _ height: CGFloat) ->CGRect { return CGRect(x: x, y: y, width: width, height: height) } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) textField.becomeFirstResponder() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } @objc func picker(_ sender:UIDatePicker){ let formatter = DateFormatter() formatter.dateFormat = "yyyy/MM/dd" selectLabel.text = formatter.string(from: sender.date) view.addSubview(selectLabel) } @IBAction func addEvent(_ sender: Any) { print("データ書き込み開始") let realm = try! Realm() try! realm.write { //日付表示の内容とスケジュール入力の内容が書き込まれる。 let Events = [Event(value: ["date": selectLabel.text, "event": eventTF.text])] realm.add(Events) print("データ書き込み中") } print("データ書き込み完了") //前のページに戻る dismiss(animated: true, completion: nil) self.navigationController?.popViewController(animated: true) } }
swift = 5
MACOS = 10.15.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/13 03:54