I haven't a whole lot of code, so I might as well copy it here.
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
var buildings = ["BankBuilding", "Cinema" , "CornerShop", "Greg's House"]
#IBOutlet weak var buildText: UITextField!
var buildPickers:UIPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
buildPickers = UIPickerView()
buildPickers.delegate = self
buildPickers.hidden = true;
buildText.inputView = buildPickers
buildText.text = buildings[0]
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
println("Count: \(buildings.count)")
return buildings.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
println("creating title: \(buildings[row])")
return buildings[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
println("Selected: \(buildings[row])")
buildText.text = buildings[row]
buildPickers.hidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
buildPickers.hidden = false
return false
}
}
The print statements are correct. For numberOfRows... and titleForRow it is printing the correct Strings.
But there is no prints for didSelectRow because, well, I can't select a row.
This is what I get:
You can ignore the Google Map in the background, that shouldn't interfere with the Picker View and is just set up in the StoryBoard.
The Grey window appears when I click on the textField but never shows any content. But the print statements say otherwise.
Does anyone know why this is the case?
Just add this line in your viewDidLoad method:
buildPickers.dataSource = self
And your code will be:
override func viewDidLoad() {
super.viewDidLoad()
buildPickers = UIPickerView()
buildPickers.delegate = self
buildPickers.dataSource = self
buildPickers.hidden = true;
buildText.inputView = buildPickers
buildText.text = buildings[0]
}
And it will show your data.
UPDATE:
It is not showing because you set it hidden in your viewDidLoad.
Just remove this line from your code:
buildPickers.hidden = true
Here is your working code:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate{
var buildings = ["BankBuilding", "Cinema" , "CornerShop", "Greg's House"]
#IBOutlet weak var buildText: UITextField!
var buildPickers:UIPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
buildPickers = UIPickerView()
buildPickers.delegate = self
buildPickers.hidden = true
buildText.delegate = self //set delegate for textField
buildText.inputView = buildPickers
buildText.text = buildings[0]
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
println("Count: \(buildings.count)")
return buildings.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
println("creating title: \(buildings[row])")
return buildings[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
println("Selected: \(buildings[row])")
buildText.text = buildings[row]
buildPickers.hidden = true;
}
//this method will call when you click on textField
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
buildPickers.hidden = false
return true
}
}
Is the delegate set on the text field? Are you getting the callback which you use to un-hide the picker?
Related
I have attached my code for your reference. I can able to display the data in Pickerview as i change the row the data changes accordingly but the data which selected from the picker is not getting displayed in the UITextfield. if I select any option in Pickerview I could not see any text inside the uitextfield if I select it still showing blank text field.
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
#IBOutlet weak var textfield1: UITextField!
#IBOutlet weak var textfield2: UITextField!
var x = 0
let leaguepickerview_destination = UIPickerView()
let clubpickerview_denomination = UIPickerView()
let league_destination = ["India","Europe","Australia","New Zealand","USA & Rest of the World"]
let club_denomination = [[" Rs.500","Rs.1000","Rs.1500","Rs.2000","Rs.2500","Rs.3000","Rs.3500","Rs.4000","Rs.4500","Rs.5000"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"]]
override func viewDidLoad() {
super.viewDidLoad()
leaguepickerview_destination.delegate = self
leaguepickerview_destination.dataSource = self
clubpickerview_denomination.delegate = self
clubpickerview_denomination.dataSource = self
textfield1.inputView = leaguepickerview_destination
textfield2.inputView = clubpickerview_denomination
textfield1.delegate = self
textfield2.delegate = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch (pickerView){
case leaguepickerview_destination:
return league_destination.count
case clubpickerview_denomination:
return club_denomination[x].count
default:
return 0
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch (pickerView){
case leaguepickerview_destination:
return league_destination[row]
case clubpickerview_denomination:
return club_denomination[x][row]
default:
return "an error occurred"
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView == leaguepickerview_destination) {
x = row
clubpickerview_denomination.reloadAllComponents()
}
}
}
You need to set the textfield text
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView == leaguepickerview_destination) {
x = row
clubpickerview_denomination.reloadAllComponents()
textfield1.text = league_destination[row]
}
else {
textfield2.text = club_denomination[x][row]
}
}
The text field delegate method textFieldDidEndEditing is not firing when picker view is used in Swift 3. How can I call it? There is no button to dynamically call the delegate.
You can see my code below.
import UIKit
class ThroewViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate {
#IBOutlet weak var pickerText: UITextField!
let thePicker = UIPickerView()
var dashTitle_Arr = ["One","Two","Three","Four","Five","Six"]
override func viewDidLoad() {
super.viewDidLoad()
pickerText.delegate = self
thePicker.dataSource = self
thePicker.delegate = self
pickerText.inputView = thePicker
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dashTitle_Arr .count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dashTitle_Arr [row] }
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerText.text = dashTitle_Arr [row]
}
func textFieldDidEndEditing(_ textField: UITextField) {
addIntoArray(text: textField.text)
}
func addIntoArray(text: String?) {
guard let text = text, text.characters.count > 0 else {
return
}
dashTitle_Arr.append(text)
thePicker.reloadAllComponents()
}
}
There is a property of picker-view is accessoriesView. Create a tool bar with done or cancel button and set it in the picker view's assessoriesView.
In the toolbar button action write the textfield.resignFirstResponder()
After that the textFieldDidEndEditing delegate method fired
It works fine for me. Hope it will helps you. Thank you
I am trying to create a separate pickerview for each textbox that is selected. so far the closest I have gotten is having the same pickerview for each of the text boxes that need their own separate list of items to choose from.
class MainViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {
var dogBreedPickerArray = ["German Shepherd", "Huskie", "cray cray"]
var catBreedPickerArray = ["savanna", "cheetoh", "bengal"]
var maleFemalePickerArray = ["Male", "Female"]
var dogBreedPicker = UIPickerView()
var dogBreedPicker = UIPickerView()
var catBreedPicker = UIPickerView()
var maleFemalePicker = UIPickerView()
#IBOutlet var dogBreedTextBox: UITextField!
#IBOutlet var catBreedTextBox: UITextField!
#IBOutlet var maleFemaleTextBox: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
dogBreedPicker.delegate = self
dogBreedPicker.dataSource = self
dogBreedTextBox.inputView = dogBreedPicker
dogBreedTextBox.delegate = self
}
// dog picker functions
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dogBreedPickerArray.count
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
petBreedTextBox.text = dogBreedPickerArray[row]
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dogBreedPickerArray[row]
}
}
its just a example may be it help to you
after selecting any text field asign textfield tag to picker and reload the component.
// variable
var heightInch:[Int] = [Int]()
var heightfeet:[Int] = [Int]()
var heightCms:[Int] = [Int]()
delegate methods
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
if picker.tag == 1{
return 2
}else{
return 1
}
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if picker.tag == 1{
switch component {
case 0:
return heightfeet.count
default:
return heightInch.count
}
}else{
return heightCms.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if picker.tag == 1{
switch component {
case 0:
return String(heightfeet[row])
default:
return String(heightInch[row])
}
}else{
return String(heightCms[row])
}
What is wrong with the code that I have produced? I am trying to get a UIPickerView to pop up when the textfield is tapped then go away once a selection is made. The textfield is supposed to present what was selected after.
import UIKit
class CreateAJob_View_ControllerViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
#IBOutlet var pickerTextField: UITextField!
#IBOutlet var picker: UIPickerView!
var pickerData:[String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
picker.hidden = true;
pickerTextField.text = "A4";
self.pickerTextField.delegate = self
self.picker.delegate = self
self.picker.dataSource = self
pickerData = ["A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","A10"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerTextField.text = pickerData[row];
picker.hidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
pickerTextField.hidden = false
return false
}
}
You need to change these two methods this way:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.pickerTextField.hidden = false;
self.picker.hidden = true;
self.pickerTextField.text = pickerData[row];
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
self.pickerTextField.hidden = true
self.picker.hidden = false;
return false
}
How can I add other picker view in my view controller
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var UnitTypeLabel: UILabel!
#IBOutlet weak var UnitTypeSelectBTN: UIButton!
#IBOutlet weak var UnitTypePicker: UIPickerView!
let UnitDataArray = ["Unit for Weight","Unit for Length","Unit for Area","Unit for Volume","Unit for Quantity"]
let UnitforLengthData = ["m","km","cm","mm","yd","ft","in"]
let UnitforWeightData = ["g","kg","m.t","l.t","sh.t","lb"]
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return UnitDataArray.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return UnitDataArray[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
return UnitTypeLabel.text = UnitDataArray[row]
}
override func viewDidLoad() {
super.viewDidLoad()
//UnitTypePicker.hidden = true
UnitTypePicker.delegate = self
UnitTypePicker.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is how it should be, you have to implement pickerView viewForRow method to achieve this:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var pickerView: UIPickerView!
let UnitDataArray = ["Unit for Weight","Unit for Length","Unit for Area","Unit for Volume","Unit for Quantity"]
override func viewDidLoad() {
super.viewDidLoad()
// pickerView delegate n dataSource
pickerView.dataSource = self
pickerView.delegate = self
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return UnitDataArray.count
}
//MARK: Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return UnitDataArray[row]
}
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
var pickerLabel = UILabel()
pickerLabel.textColor = UIColor.blackColor()
pickerLabel.text = UnitDataArray[row]
if let pickerViewFont = UIFont(name: "HelveticaNeue-Thin", size: 30) {
pickerLabel.font = pickerViewFont
}
pickerLabel.textAlignment = NSTextAlignment.Center
return pickerLabel
}
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 40.0
}
}
If you want more number of pickerView then you will have to set those number of components in it like this:
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
return 2 //number of pickerViews
}
By changing the number, you can set those number of pickerView in same viewController.
But if you want to change only dataSet for only one pickerView after any action then you can see this How do I reload/refresh the UIPickerView (with new data array) based on button press?