First iOS app in Swift - Question marks in UIPickerView - ios

I'm working on my first Swift ios application.
Can't get data from pickerData into my picker, but I only get question marks instead of real values. I guess it's something to do with delegate, but not sure what wrong.
import UIKit
import CoreData
class NewWorkoutController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var workoutDistance: UITextField!
let pickerData = ["11","12","13","14","15"]
// Data Sources
func numberOfComponentsInPickerView(distancePickerView: UIPickerView) -> Int {
return 1
}
func pickerView(distancePickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
// Delegates
func pickerViewReturnRow(distancePickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[row]
}
func pickerViewText(distancePickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
workoutDistance.text = pickerData[row]
}
func doneDistancePicker() {
workoutDistance.resignFirstResponder()
}
func cancelDistancePicker() {
workoutDistance.resignFirstResponder()
}
#IBAction func textFieldDistanceEditing2(sender: UITextField) {
// Create picker view
var distancePickerView: UIPickerView
distancePickerView = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300))
distancePickerView.backgroundColor = .whiteColor()
distancePickerView.showsSelectionIndicator = true
distancePickerView.delegate = self
distancePickerView.dataSource = self
// 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: "doneDistancePicker")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelDistancePicker")
// Assign buttons to toolbar
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
// Add pickerview and toolbar to textfield
workoutDistance.inputView = distancePickerView
workoutDistance.inputAccessoryView = toolBar
sender.inputView = distancePickerView
}
override func viewDidLoad() {
super.viewDidLoad()
}
}

The signature for this function is wrong:
func pickerViewReturnRow(distancePickerView: UIPickerView,
titleForRow row: Int,
forComponent component: Int) -> String!
It should be
func pickerView(pickerView: UIPickerView,
titleForRow row: Int,
forComponent component: Int) -> String!
As a result that method (which provides titles for the rows in your picker) isn't getting called.

In all pickerView delegate method's signature, you replaced pickerView with your pickerView outlet that is distancePickerView which is not required. You should modify code inside delegate method's body because delegate methods are automatically called by swift compiler if we confirm your pickerView delegate and datasource to your ViewController class by writing code as:
pickerViewOutlet.delegate = self
pickerViewOutlet.dataSource = self
So just modify all pickerView delegate methods with its default signature then it will get called by compiler and data will display in it.

Related

Picker buttons do not show up

I am attempting to add "done" and "cancel" buttons in a toolbar above a pickerView that will be used to change the value of a label. The label should only be changed when "done" is clicked not when UIPicker received rowSelected.
I can't understand why my "done" and "cancel" buttons do not show above my picker? I'm sure this will be marked as duplicate, but I've done something very similar in Swift 4, and this doesn't seem to work in Swift 5.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var freq: UILabel!
var freqOptions = ["Hz", "kHz", "MHz"]
var freqPicker = UIPickerView()
#IBOutlet weak var freqTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
freqPicker = UIPickerView( frame: CGRect(x: 0, y: 100, width: self.view.frame.width, height: 216) )
freqPicker.backgroundColor = UIColor.white
freqPicker.delegate = self
freqPicker.dataSource = self
freqPicker.isHidden = true
let toolBar = UIToolbar()
let cancelItem = UIBarButtonItem(
title: "Cancel",
style: .plain,
target: self,
action: #selector(cancelFreqInput)
)
let middleItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneItem = UIBarButtonItem(
title: "Done",
style: .done,
target: self,
action: #selector(confirmFreqInput)
)
toolBar.setItems( [cancelItem, middleItem, doneItem], animated : false )
toolBar.sizeToFit()
view.addSubview(freqPicker)
let freqTap = UITapGestureRecognizer(target: self, action: #selector(freqTap(gestureReconizer:)))
freq.addGestureRecognizer(freqTap)
freqTextField.inputView = freqPicker
freqTextField.inputAccessoryView = toolBar
freqTextField.isHidden = true
freq.isUserInteractionEnabled = true
}
#objc func cancelFreqInput() {
freqTextField.resignFirstResponder()
freq.text = "Hz"
freqTextField.isHidden = true
}
#objc func confirmFreqInput() {
freqTextField.resignFirstResponder()
freqTextField.isHidden = true
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return freqOptions.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
freq.text = freqOptions[row]
self.view.endEditing(true)
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return freqOptions[row]
}
#objc func freqTap(gestureReconizer: UITapGestureRecognizer) {
freqPicker.isHidden = false
}
}
you don't need to add a picker as a subview. You just need to set it Like an input view (and you already done it). I modified you code. You can just compare and check what was wrong there
class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var freq: UILabel!
var freqOptions = ["Hz", "kHz", "MHz"]
var freqPicker = UIPickerView()
#IBOutlet weak var freqTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
freqPicker = UIPickerView()
freqPicker.backgroundColor = UIColor.white
freqPicker.delegate = self
freqPicker.dataSource = self
// freqPicker.isHidden = true
let toolBar = UIToolbar()
let cancelItem = UIBarButtonItem(
title: "Cancel",
style: .plain,
target: self,
action: #selector(cancelFreqInput)
)
let middleItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneItem = UIBarButtonItem(
title: "Done",
style: .done,
target: self,
action: #selector(confirmFreqInput)
)
toolBar.setItems( [cancelItem, middleItem, doneItem], animated : false )
toolBar.sizeToFit()
// view.addSubview(freqPicker)
let freqTap = UITapGestureRecognizer(target: self, action: #selector(freqTap(gestureReconizer:)))
freq.addGestureRecognizer(freqTap)
freqTextField.inputView = freqPicker
freqTextField.inputAccessoryView = toolBar
freqTextField.isHidden = true
freq.isUserInteractionEnabled = true
freqPicker.reloadComponent(0)
}
#objc func cancelFreqInput() {
freqTextField.resignFirstResponder()
freq.text = "Hz"
freqTextField.isHidden = true
}
#objc func confirmFreqInput() {
freq.text = freqOptions[freqPicker.selectedRow(inComponent: 0)]
self.view.endEditing(true)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return freqOptions.count
}
// func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// freq.text = freqOptions[row]
// self.view.endEditing(true)
// }
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return freqOptions[row]
}
#objc func freqTap(gestureReconizer: UITapGestureRecognizer) {
freqTextField.becomeFirstResponder()
}
}

Swift - Programmatically create UIPickerView

I created PickerView, but this does not open the PickerView I created. Where do I make a mistake? Why PickerView doesn't open. I added picker view in the text field but it doesn't work. I added it to viewDidLoad, but when I click a text field, picker view does not open.
class EnergyChart: UIViewController , UIPickerViewDelegate, UIPickerViewDataSource {
var picker = UIPickerView()
var gradePickerValues1 : [String] = ["...", ...]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return gradePickerValues1.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return gradePickerValues1[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
getDevice.text = gradePickerValues1[row]
switch row {
case 0: break
default: break
}
self.view.endEditing(true)
}
#objc func cancelTapped() {
self.view.endEditing(true)
}
let getDevice: UITextField = {
let textFieldframe = CGRect()
let textField2 = SkyFloatingLabelTextFieldWithIcon(frame: textFieldframe, iconType: .image)
...
return textField2
}()
#objc func GetDevice() {
self.getDevice.text = self.gradePickerValues1[0]
}
override func viewDidLoad() {
super.viewDidLoad()
picker.backgroundColor = .white
picker.showsSelectionIndicator = true
picker.delegate = self
picker.dataSource = self
getDevice.inputView = picker
GetDevice()
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(..)
toolBar.sizeToFit()
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "İptal", style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancelTapped))
toolBar.setItems([cancelButton, spaceButton], animated: false)
toolBar.isUserInteractionEnabled = true
getDevice.inputAccessoryView = toolBar
view.addSubview(getDevice)
getDevice.snp.makeConstraints { (make) in
make.centerX.equalTo(view).offset(50)
make.top.equalTo(view).offset(510)
make.height.equalTo(50)
make.width.equalTo(200)
}

UIPickerView won't select the first row unless I select a different row first?

It seems that I cannot select the very first row in the UIPickerView when I first load it. I want the textfield to update immediately when I load the picker view. It only updates when I select something else, and then go back to the first row.
var platformData = ["XBOX", "PS4", "PC"]
var picker = UIPickerView()
#IBOutlet var platformSelected: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
platformSelected.inputView = picker
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneAction))
toolbar.items = [doneButton]
platformSelected.inputAccessoryView = toolbar
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return platformData.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
platformSelected.text = platformData[row]
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return platformData[row]
}
Improve your code.
platformSelected.delegate = self
......
// MARK: - UITextFieldDelegate
extension YourUIViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
// TODO: - Specify index what you need. After editing it changed and you should get selected index using its position in platformData Array
let index = 0
picker.selectRow(index, inComponent: 0, animated: true)
}
}

Determining which UIPicker's toolbar button sent a message

I am trying to use multiple UIPickers on a page. I think I've configured almost everything. When I use a UIPicker I add in a a tool bar with a "Cancel" and a "Done" Button.
The toolbars are built using the same function, so it calls the same action when a user taps "Done". A pseudo example flow below.
let pickerData: [String] = ["1", "2", "3", "4"]
var picker = UIPickerView()
let pickerData2: [String] = ["A", "B", "C", "D"]
var picker2 = UIPickerView()
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var textField2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// set up pickers
setPickerView()
}
// set the pickerview
func setPickerView(){
picker.dataSource = self
picker.delegate = self
textField.inputView = picker
textField.inputAccessoryView = initToolBar()
// second picker
picker2.dataSource = self
picker2.delegate = self
textField2.inputView = picker
textField2.inputAccessoryView = initToolBar()
}
// build the toolbar for uipicker, so a user can select a value
func initToolBar() -> UIToolbar {
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red:14.0/255, green:122.0/255, blue:254.0/255, alpha: 1)
toolBar.sizeToFit()
// TODO need to update actions for all buttons
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: nil)
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(pickerDoneAction))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
return toolBar
}
func pickerDoneAction(sender: UIBarButtonItem){
/*Here I'm just updating the same textfield when the done button is
is pressed, I'm not sure how I can get the instance of the picker into this method*/
let indexPath = picker.selectedRow(inComponent: 0)
textfield.text = data[indexPath]
}
// delegates
func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 }
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == picker {
return pickerData.count
} else return pickerData2.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == picker {
return pickerData[row]
} else return pickerData2[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == picker {
textField.text = pickerData[row]
} else textField2.text = pickerData2[row]
}
Is there a way to determine which UIPicker was being used when the done button was clicked, so I could use that instance in my pickerDoneAction method and then change the corresponding textfield's value? I tried checking if the textfield was firstResponder and even tried to check the UIPicker itself, but it didn't seem possible. Do I need to change my approach and if so how?
Update
I've added in my existing UIPicikerView delegate methods.
Just to clarify, I want the value of the UIPicker to be assigned to the correct textfield when a user presses "Done", therefore if a user wants to select the very first row from a UIPicker, it gets inserted.
You can implement the following UIPickerViewDelegate's method:
optional func pickerView(_ pickerView: UIPickerView,
didSelectRow row: Int,
inComponent component: Int)
where the method is Called by the picker view when the user selects a row in a component and use
pickerView
(An object representing the picker view requesting the data) to know which pickerview has been used.
You can always pass down data using UIView's tag property:
func setPickerView() {
picker.tag = 1
textField.inputView = picker
textField.inputAccessoryView = initToolBar(pickerTag: picker.tag)
picker2.tag = 2
textField.inputView = picker2
textField.inputAccessoryView = initToolBar(pickerTag: picker2.tag)
}
func initToolBar(pickerTag: Int) {
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(pickerDoneAction))
doneButton.tag = pickerTag
}
func pickerDoneAction(sender: UIBarButtonItem){
let pickerView: UIPickerView
switch sender.tag {
case picker.tag: pickerView = picker
case picker2.tag: pickerView = picker2
default: return
}
}
You can try changing data dynamically with different conditions that you set as data source to pickerview
Maybe you don't need to determine which picker it is, but which text field.
In pickerDoneAction(sender:), use let textField = sender.superview.superview, where sender is the button, parent is the toolbar and the toolbar's parent is the text field. Then you fill it with pickerData[0].
Another way to achieve the same goal is to use UITextFieldDelegate's method textFieldDidEndEditing(_:). When the text field ends editing, insert the default value there.
let pickerData: [String] = ["1", "2", "3", "4"]
var picker = UIPickerView()
let pickerData2: [String] = ["A", "B", "C", "D"]
var picker2 = UIPickerView() // <<<<<<<<<<<<<
var userPicker:UIPickerView?
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var textField2: UITextField!
.....
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
userPicker = pickerView // <<<<<<<<<<<<<
if pickerView == picker {
return pickerData[row]
} else return pickerData2[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
userPicker = pickerView // <<<<<<<<<<<<<
if pickerView == picker {
textField.text = pickerData[row]
} else textField2.text = pickerData2[row]
}
You can simply check which textfield is in editing state. Do this in your done clicked function. You don't have to add any tags to the pickerView or to the textField.
func pickerDoneAction(sender: UIBarButtonItem) {
if textField1.isEditing == true {
Print("pickerview 1 is selected")
} else {
Print("pickerview 2 is selected")
}
}
Hope this helps! :)

fatal error: Index out of range in swift while taking options from uipicker

I have 3 text field like to select gender, country, state. And I am using UIPicker to get the input values from UIPicker. But now when I press gender UITextField to open UIPickerenter code here, the UIPickerenter code here is showing with no data. But the same UIPicker I am using for the country, state. that time it's showing the data with UIPicker for both countries, state text field. Can anyone please help me out. How to solve this problem.
Error :
fatal error: Index out of range
If I press in uipicker option view only its crashing.
My code :
I am getting a crash in this class file :
import Foundation
import SwiftCountryPicker
class DataPicker: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
var dataPicker: UIPickerView!
var inputText:UITextField!
var parent:UIViewController!
var pickerData: [String]!
var feetStr = String()
var inchStr = String()
private var _selectedValue: String!
var selectedvalue: String {
return _selectedValue ?? String()
}
func showPicker(parent:UIViewController,inputText:UITextField, data: [String], selectedValueIndex: Int){
self.inputText = inputText
self.parent = parent
pickerData = data
if dataPicker == nil {
dataPicker = UIPickerView(frame: CGRectMake(0,0,parent.view.frame.size.width, 216))
dataPicker.backgroundColor = UIColor.whiteColor()
dataPicker.dataSource = self
dataPicker.delegate = self
dataPicker.selectRow(selectedValueIndex, inComponent: 0, animated: true)
_selectedValue = pickerData.count > 0 ? pickerData[selectedValueIndex] : ""
}
inputText.inputView = dataPicker
// ToolBar
let toolBar = UIToolbar()
toolBar.barStyle = .Default
toolBar.translucent = true
toolBar.tintColor = UIColor(hex: "B12420")
toolBar.backgroundColor = UIColor.whiteColor()
toolBar.sizeToFit()
// Adding Button ToolBar
let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: #selector(DataPicker.doneClick))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(DataPicker.cancelClick))
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
inputText.inputAccessoryView = toolBar
}
func doneClick() {
inputText.text = _selectedValue
inputText.resignFirstResponder()
}
func cancelClick() {
inputText.resignFirstResponder()
}
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) {
_selectedValue = pickerData[row]
}
}
In this line :
func pickerView(pickerView: UIPickerView, didSelectRow row: Int,
inComponent component: Int) {
_selectedValue = pickerData[row]
}
And my text field delegate for press the texfield:
extension UserInfoViewController:UITextFieldDelegate{
func textFieldDidBeginEditing(textField: UITextField) {
selectedText = textField
else if textField == genderss {
ctDataPicker = DataPicker()
let indexPos = genders.indexOf(genderss.text!) ?? 0
ctDataPicker.showPicker(self, inputText: textField, data: genders,selectedValueIndex: indexPos)
}
}
}
UIPickerViewDataSource method didSelectRow is changed in Swift 4:
func numberOfComponents(in 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) {
TextField.text = myPickerData[row]
}
Try changing the line crashing _selectedValue to a textfield example
TextField.text = myPickerData[row]
Check Apple Documentation on UIPickerView.

Resources