如何在swift3中为DatePicker DONE按钮编写选择器方法

时间:2017-06-22 13:39:42

标签: ios swift3 uidatepicker

虽然我运行的代码没有显示任何错误但在控制台中显示

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:   ' - [MapSample.SampleViewController dateTextField:]:无法识别   选择器发送到实例

这里我在故事板中使用了textfield,我编写了如下代码。在错误中帮助我

import UIKit

class SampleViewController: UIViewController {


    @IBOutlet weak var dateTextField: UITextField!
    let datePicker = UIDatePicker()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.creatDatePickerView()

    }

    func creatDatePickerView(){

        datePicker.datePickerMode = .date
        let toolBar = UIToolbar()
        toolBar.sizeToFit()
               let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(donePicker))
        toolBar.setItems([doneButton], animated: false)

        dateTextField.inputAccessoryView = toolBar
        dateTextField.inputView = datePicker

    }


    func donePicker()
    {
        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = .short
        dateTextField.text = dateFormatter.string(from: datePicker.date)
        self.view.endEditing(true)
        print("done pressed")

    }

}

0 个答案:

没有答案