Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 views
in Technique[技术] by (71.8m points)

uitextfield - Set date and time is not displayed in textField / Swift

I created a UITextField and a datePicker as InputView. Everything works except the displaying of the set date and time.

This is my Code:

func setDatePickerAsInputView(target: Any, selector: Selector) {
        
        let screenWidht = UIScreen.main.bounds.width
        let datePicker = UIDatePicker(frame: CGRect(x: 0.0, y: 0.0, width: screenWidht, height: 300))
        datePicker.datePickerMode = .dateAndTime
        self.inputView = datePicker
        
        let toolBar = UIToolbar(frame: CGRect(x: 0.0, y: 0.0, width: screenWidht, height: 30.0))
        self.inputAccessoryView = toolBar
        
        
    }

func setUpDateTextField() {
        
        //DateTextField
        dateTextField.layer.cornerRadius = 25
        dateTextField.layer.borderWidth = 2
        dateTextField.textAlignment = .center
        self.dateTextField.setDatePickerAsInputView(target: self, selector: #selector(dateSelected))
        
        dateTextField.translatesAutoresizingMaskIntoConstraints = false
        dateTextField.widthAnchor.constraint(equalToConstant: 200).isActive = true
        dateTextField.heightAnchor.constraint(equalToConstant: 80).isActive = true
        
        
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .short
        
        dateTextField.text = formatter.string(from: datePicker.date)
    }
    
    @objc func dateSelected() {
        if let datePicker = self.dateTextField.inputView as? UIDatePicker {
            let dateFormatter = DateFormatter()
            dateFormatter.dateStyle = .medium
            dateFormatter.timeStyle = .short
            self.dateTextField.text = dateFormatter.string(from: datePicker.date)
            dateTextField.text = "(datePicker.date)"
        }
        
        self.dateTextField.resignFirstResponder()
        
    }
    

But if I close the datePicker it's still the current (old) date in the TextField and not the date and time I set. Can some help me? Thanks in advance!

question from:https://stackoverflow.com/questions/65949026/set-date-and-time-is-not-displayed-in-textfield-swift

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have missed assign target to UIDatePicker````. As you are passing target and selector but not assing to UIDatePicker```

Add this line inside the setDatePickerAsInputView function

datePicker.addTarget(target, action: selector, for: .valueChanged)

Now, dateSelected method call everytime whenever value is changed. So, you need to remove this line self.dateTextField.resignFirstResponder() from function. Or you can change action from .valueChanged to .editingDidEnd


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...