I want the UIPickerView to be dismissed (slide out of view) after the done button is clicked on the toolbar. I've linked the UIPickerView as the inputView of the UITextField and set the UIToolbar as the inputAccessoryView of the UIPickerView. However, my function calling resignFirstResponder does not slide the UIPicker out of the view. Any help is appreciated and thanks in advance!
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
// Do any additional setup after loading the view, typically from a nib.
//initialization constants
let pickerView = UIPickerView()
let textField = UITextField()
let pickerData = ["1","2","three"]
override func viewDidLoad() {
//pickerview tool bar
let toolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 44))
var items = [AnyObject]()
//making done button
let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: Selector(donePressed()))
items.append(doneButton)
toolbar.barStyle = UIBarStyle.Black
toolbar.setItems(items, animated: true)
//creating textfields with a pickerview
pickerView.delegate = self
pickerView.dataSource = self
pickerView.frame = CGRectMake(0, 0, 500, 300)
textField.inputAccessoryView = toolbar
textField.inputView = pickerView
textField.frame = CGRectMake(200, 55, 100, 35)
textField.backgroundColor = UIColor.blueColor()
//adding objs to viewController
self.view.addSubview(textField)
}
func donePressed() {
resignFirstResponder()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//MARK: Data Sources UIPickerView
extension ViewController: UIPickerViewDataSource {
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
}
//MARK: Delegates UIPickerView
extension ViewController: UIPickerViewDelegate {
// several optional methods:
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {
textField.text = pickerData[row]
}
}
There are 2 problems:
Your selector for donePressed
is declared incorrectly. It should be:
let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "donePressed")
You need to call resignFirstResponder()
on textField
:
func donePressed() {
textField.resignFirstResponder()
}
If you have multiple textFields, and you don't want to hardcode the textField in the resignFirstResponder
call, you can do the following:
Make your ViewController
a UITextFieldDeletate
:
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {
Set the delegate
property on your UITextField
:
textField.delegate = self
Add a property to your ViewController
to keep track of the active UITextField
:
var activeTextField:UITextField?
Implement textFieldDidBeginEditing
and store the active UITextField
:
func textFieldDidBeginEditing(textField: UITextField) { // became first responder
activeTextField = textField
}
In donePressed
, call resignFirstResponder
on activeTextField
:
func donePressed() {
activeTextField?.resignFirstResponder()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With