点击外部

时间:2016-12-07 00:45:25

标签: ios swift datepicker

在我的应用程序中,我有一个文本字段来添加出生日期。一旦用户点击文本字段,我就会弹出一个datePicker。 datePicker正确显示,并在用户选择它在文本字段中显示的日期后显示。问题是在选择datePicker未关闭的日期之后。

这是我的代码:

class BController: UIViewController, UITextFieldDelegate {

 @IBOutlet weak var dateOfBirth: UITextField!

    let datePicker = UIDatePicker()




    func textFieldShouldReturn(textField: UITextField) -> Bool {
         textField.resignFirstResponder()
       // self.view.endEditing(true)
        return true
    }



    func textFieldDidBeginEditing(textField: UITextField){
         if textField == dateOfBirth{
           // let datePicker = UIDatePicker()

            datePicker.datePickerMode = UIDatePickerMode.Date

            datePicker.backgroundColor = UIColor.blackColor()
            datePicker.setValue(UIColor.whiteColor(), forKey: "textColor")

            textField.inputView = datePicker
            datePicker.addTarget(self, action: "datePickerChanged:", forControlEvents: .ValueChanged)
        }
    }

    func datePickerChanged(sender: UIDatePicker){

        let formatter = NSDateFormatter()
        formatter.dateFormat = "dd-MM-yyyy"
        //    formatter.dateStyle = .ShortStyle
        //  formatter.timeStyle = .NoStyle
        dateOfBirth.text = formatter.stringFromDate(sender.date)

    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        print("touch happened")
        self.view.endEditing(true)

    }


    override func viewDidLoad() {
        super.viewDidLoad()
        dateOfBirth.delegate = self
        text1.delegate =  self
        text2.delegate = self

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



}

我得到了控制台输出:

  

无法识别的选择器发送到实例0x7由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [BController getCalendar:]:无法识别的选择器发送到实例0x78789050'

如何解决这个问题?我正在使用Xcode 6.2。如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

正如文件所说:

  

var inputView:UIView?   当文本字段成为第一个响应者时显示的自定义输入视图。

因此,您应首先初始化datePicker,然后将其分配给viewDidLoad方法中的dateOfBirth的inputView属性,而不是textFieldDidBeginEditing方法。

要在外部关闭datePicker,请在self.view上添加UITapGestureRecognizer

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let datePicker = UIDatePicker.init()
        textField.inputView = datePicker

        let gestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(backgroundTap(gesture:)));
        view.addGestureRecognizer(gestureRecognizer)

    }

    func backgroundTap(gesture : UITapGestureRecognizer) {
        textField.resignFirstResponder() // or view.endEditing(true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

你的错误是当textFieldDidBeginEditing方法调用时,UITextField对UIDatePicker一无所知。