kareem kareem - 4 months ago 31
iOS Question

How to hide keyboard when you select UIPickerView in Swift

I have a DatePicker where the user selects time and date fields, the keyboard shows up first and then I have to click the textfield again for the Picker to appear. What I want is for the DatePicker to show up on the first try.

here is my code

@IBOutlet var dateField: UITextField! = UITextField()

@IBOutlet var timeField: UITextField! = UITextField()

@IBAction func userPickDate(sender: UITextField) {

var datePickerView : UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)

}
@IBAction func userPickTime(sender: UITextField) {

var datePickerView : UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Time
sender.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("handleTimePicker:"), forControlEvents: UIControlEvents.ValueChanged)

}

func handleTimePicker(sender: UIDatePicker)
{
var timeFormatter = NSDateFormatter()
timeFormatter.timeStyle = .ShortStyle
timeField.text = timeFormatter.stringFromDate(sender.date)

}



func handleDatePicker(sender: UIDatePicker) {
var timeFormatter = NSDateFormatter()
timeFormatter.dateStyle = .MediumStyle
//timeFormatter.timeStyle = .ShortStyle
dateField.text = timeFormatter.stringFromDate(sender.date)


}

Answer

Discard the last answer. I understood whAT you are doing. Here is what I found: Placing the declaration of the UPickerView objects in the scope of the class rather than localized to the handler seemed to work. Here is the code. Note the addition of the resignFirstResponder().

import UIKit

class ViewController: UIViewController {
    var datePickerView  : UIDatePicker = UIDatePicker()
    var timePickerView  : UIDatePicker = UIDatePicker()
    @IBOutlet weak var dateField: UITextField!
    @IBOutlet weak var timeField: UITextField!

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

    datePickerView.datePickerMode = UIDatePickerMode.Date
    dateField.inputView = datePickerView
    datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)
    handleDatePicker(datePickerView)


    timePickerView.datePickerMode = UIDatePickerMode.Time
    timeField.inputView = timePickerView
    timePickerView.addTarget(self, action: Selector("handleTimePicker:"), forControlEvents: UIControlEvents.ValueChanged)
    handleTimePicker(timePickerView)


}

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




func handleDatePicker(sender: UIDatePicker) {
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = .MediumStyle
    //timeFormatter.timeStyle = .ShortStyle
    dateField.text = dateFormatter.stringFromDate(sender.date)
    dateField.resignFirstResponder()

}

func handleTimePicker(sender: UIDatePicker) {

    var timeFormatter = NSDateFormatter()
    timeFormatter.timeStyle = .ShortStyle
    timeField.text = timeFormatter.stringFromDate(sender.date)
    timeField.resignFirstResponder()

}

}

Comments