I am currently trying to create a done button for a UIPickerView.
I believe that I have added in the code for a done button to show up but a done button does not show up in the simulator.
Here is my code:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UITextFieldDelegate, UIPickerViewDataSource {
@IBOutlet weak var genderTextField: UITextField!
@IBOutlet weak var getGender: UIPickerView!
@IBOutlet weak var preferenceTextField: UITextField!
@IBOutlet weak var getPreference: UIPickerView!
let gender = ["", "Male", "Female"]
let preference = ["", "Male", "Female", "Both"]
override func viewDidLoad() {
super.viewDidLoad()
var theWidth = view.frame.size.width
var elWidth = theWidth - 50
// Adds title text field to the top
var titleLabel: UILabel = UILabel()
titleLabel.frame = CGRectMake(25, 40, elWidth, 40)
titleLabel.textColor = UIColor(red: 1.0, green: (203.0/255.0), blue: (38.0/255.0), alpha: 1.0)
titleLabel.font = UIFont(name: "Apple SD Gothic Neo", size: 28.0)
titleLabel.textAlignment = NSTextAlignment.Center
titleLabel.text = "Set up your profile"
self.view.addSubview(titleLabel)
getGender.hidden = true;
genderTextField.placeholder = "Gender"
genderTextField.delegate = self
getGender.delegate = self
getGender.frame = CGRectMake(0,view.frame.size.height-216, view.frame.size.width, 216)
getPreference.hidden = true;
preferenceTextField.placeholder = "Gender preference"
preferenceTextField.delegate = self
getPreference.delegate = self
getPreference.frame = CGRectMake(0,view.frame.size.height-216, view.frame.size.width, 216)
var toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()
var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "canclePicker")
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
genderTextField.inputView = getGender
genderTextField.inputAccessoryView = toolBar
preferenceTextField.inputView = getPreference
preferenceTextField.inputAccessoryView = toolBar
}
@IBAction func genderTextClick(sender: UITextField) {
getGender.showsSelectionIndicator = true
// Create toolbar
var toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()
// Create buttons
var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneGenderPicker")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelGenderPicker")
// Assign buttons to toolbar
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
// Add pickerview and toolbar to textfield
genderTextField.inputView = getGender
genderTextField.inputAccessoryView = toolBar
sender.inputView = getGender
}
@IBAction func preferenceTextClick(sender: UITextField) {
getPreference.showsSelectionIndicator = true
// Create toolbar
var toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()
// Create buttons
var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePreferencePicker")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelPreferencePicker")
// Assign buttons to toolbar
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
// Add pickerview and toolbar to textfield
preferenceTextField.inputView = getPreference
preferenceTextField.inputAccessoryView = toolBar
sender.inputView = getPreference
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
getGender.tag = 0
getPreference.tag = 1
if pickerView.tag == 0 {
return gender.count
} else if pickerView.tag == 1 {
return preference.count
}
return 1
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
getGender.tag = 0
getPreference.tag = 1
if pickerView.tag == 0 {
return gender[row]
} else if pickerView.tag == 1 {
return preference[row]
}
return ""
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
getGender.tag = 0
getPreference.tag = 1
if pickerView.tag == 0 {
genderTextField.text = gender[row]
// getGender.hidden = true
} else if pickerView.tag == 1 {
preferenceTextField.text = preference[row]
// getPreference.hidden = true
}
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if (textField == self.genderTextField) {
getGender.hidden = false
} else if (textField == self.preferenceTextField) {
getPreference.hidden = false
}
return false
}
func doneGenderPicker() {
genderTextField.resignFirstResponder()
}
func cancelGenderPicker() {
genderTextField.resignFirstResponder()
}
func donePreferencePicker() {
preferenceTextField.resignFirstResponder()
}
func cancelPreferencePicker() {
preferenceTextField.resignFirstResponder()
}
// override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
//
// getGender.hidden = true
// getPreference.hidden = true
// }
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
so much!
Assign toolbar to textfield in textFieldShouldBeginEditing rather than on the IBAction. preferenceTextClick(_: UITextField) probably gets called later than that and hence, doesn't load the toolbar.