How to connect pickerviews and labels in iOS? - ios

I have two pickerView and two Label on my ViewController.
The question is how I could connect labels to pickerView's data array (string type) and show in labels chosen pickerView cell?
#IBOutlet weak var valuetype1: UILabel!
var pickerData: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.picker1.delegate = self
self.picker1.dataSource = self
self.picker2.delegate = self
self.picker2.dataSource = self
pickerData = ["Годы", "Месяцы", "Недели", "Дни", "Часы", "Минуты"]
}
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) {
valuetype1.text = pickerData[row]
valuetype2.text = pickerData[row]
}
How could you see I connected labels (valuetype(1..2)) to pickerData but it always show in both labels one String. I need to show in first label String chosen in pickerView1 and another string in second label, chosen in pickerView2.

UIPickerViewDelegate has a method called pickerView(_:didSelectRow:inComponent:). Add this method to your VC class. This method gets called whenever the user selects a row.
Your implementation should look somewhat similar to this.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
valuetype1.text = pickerData[row]
}
For more information refer to the apple docs: https://developer.apple.com/documentation/uikit/uipickerviewdelegate/1614371-pickerview
EDIT:
When you are using the same controller as a delegate for multiple UIPickerViews, then you can check which picker view triggered the method call simply by comparing the pickerView argument on the pickerView(_:didSelectRow:inComponent:) method.
Try this:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == picker1 {
valuetype1.text = pickerData[row]
} else if pickerView == picker2 {
valuetype2.text = pickerData[row]
}
}

Related

Get selected value from picker view (picker wheel)

I'm currently using XCode version 11.3.1 (11C505) and Swift 5 and I can't seem to find a good answer as to how to get the selected value from a picker wheel (view). Here's the relevant code from my ViewController.swift
class ViewController: UIViewController {
#IBOutlet weak var numberPicker: UIPickerView!
#IBOutlet weak var test: UILabel!
private let possibleNums: [Int] = Array(1...16) // Create an array of Ints from 1 to 16
override func viewDidLoad() {
super.viewDidLoad()
numberPicker.dataSource = self
numberPicker.delegate = self
}
}
extension ViewController: UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return possibleNums.count
}
// Attempt at getting the selected value, didn't work
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int {
print(possibleNums[numberPicker.selectedRow(inComponent: 0)])
test.text = "\(possibleNums[numberPicker.selectedRow(inComponent: 0)])"
}
}
extension ViewController: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return String(possibleNums[row])
}
}
Edit: Solution was also in comments so I figured I'd put it here in case anyone else finds this:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int should be func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int (missing _)
and test.text = "\(possibleNums[numberPicker.selectedRow(inComponent: 0)])" should just be test.text = "\test.text = "\(possibleNums[row])"
Change test.text = "\(possibleNums[numberPicker.selectedRow(inComponent: 0)])" to this: test.text = "\(possibleNums[row])"
You only have one component, so you don't need to check for it, just return the row number as the array's index.
The easiest way to do that is to change your didSelectRow function from
test.text = "\(possibleNums[numberPicker.selectedRow(inComponent: 0)])"
to
test.text = "\(possibleNums[rows])"

UiPickerview values displayed as question mark ("?")in swift - But getting the correct value while assigning it to a textBox

//I am able to see the correct value in the text box upon scrolling down the picker view. But the picker view display is coming as "?" (question marks). I am confused with where I need to assign the pickerview as input view to the textbox . But still that won't make any sense to the error I am facing right .
I am using Xcode 9 and Swift 3.2.
class BillDetailsViewController: UIViewController ,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource{
//Variables Declaration
//Picker view outlet
#IBOutlet var datePickerView: UIPickerView!
//Picker view datasource - integer array
var integerArray = [Int](1899...2500)
//Textbox where picked value to be shown
#IBOutlet var txtPaidYear: UITextField!
//ViewLoad
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
datePickerView.isHidden = true
datePickerView.reloadAllComponents()
datePickerView.dataSource = self
datePickerView.delegate = self
txtPaidYear.inputView = datePickerView
}
//picker view delegate functions
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return integerArray.count
}
public func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
let pickerValue: String = String(integerArray[row])
return pickerValue
}
public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
txtPaidYear.text = String(integerArray[row])
self.datePickerView.reloadAllComponents()
}
}
****You have to change method like this after that it will work definitely.**
and Delete Delegate for TextField which you are using..**
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
I just removed all the TextView delgates in the class and it worked . Still need to figure out what was the exact issue . One of the textView delegate method was causing the issue it seems . I need to figure it out.Anyway issue got fixed .I just created an independent project and implemented UIPIckerView for which it was working perfect. Hence I did the same in my project removing all the textview delegate methods .
Thanks much for all your support .
Replace:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int) -> String? {}
With:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {}

Getting values for two labels from single UIPicker

Trying to fill two labels using single UIPicker. It has one column, two labels which have two buttons at the end of each label. On clicking which the UIPicker view is shown
#IBOutlet weak var userLoc1: UILabel!
#IBOutlet weak var userLoc2: UILabel!
let location = ["location1", "Location2", "Location3",
"Location4", "Location5", "Location6", "Location7"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var countRow:Int = 0
if pickerView == LocationPickerView{
countRow = self.location.count
}
return countRow
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == LocationPickerView
{
let titleLocation = location[row]
return titleLocation
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == LocationPickerView
{
selectedLocation1 = self.location[row]
self.userLoc1.text = self.location[row]
self.LocationPickerView.isHidden = true
//anything that can be done here that can assign different values to
//two label using the same UIPicker
}
Any guide to resolve the matter is greatly appreciated
Just add an extra column to your picker view and then add a second array to fill that new column.
It's all the same methods used to initialise the pickerview just with a bit of added logic.
let location1 = ["location1", "Location2", "Location3",
"Location4", "Location5", "Location6", "Location7"]
let location2 = ["location8", "Location9", "Location10",
"Location11", "Location12", "Location13", "Location14"]
// Set number of columns in your picker view
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
// Set number of rows of each column to length of arrays
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch component {
case 0:
return self.location1
case 1:
return self.location2
default: break
}
}
// Set content of rows in each column
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch component {
case 0:
return self.location1[row]
case 1:
return self.location2[row]
default: break
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == LocationPickerView
{
switch component {
case 0:
selectedLocation1 = self.location1[row]
self.userLoc1.text = self.location1[row]
case 1:
selectedLocation2 = self.location2[row]
self.userLoc2.text = self.location2[row]
default: break
}
self.LocationPickerView.isHidden = true
}

Update UITableViewCell

I've got a problem. I have this tableview and this pickerView
and this is the pickerView functions of the viewcontroller:
let gender = ["M","F"]
#IBOutlet weak var dataTableView: UITableView!
//pickerFuncs
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return gender[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return gender.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(gender[row])
}
How to modify the "m" in the cell with the selection of the uipicker? thanks everybody!
Create a variable
var currentGender = "M"
Set your TableViewCell with currentGender like
cell.gender = currentGender
When you select a row in UIPickerView,
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
currentGender = gender[row]
// Then reload your TableView row
let indexPath = IndexPath(item: rowNumber, section: 0)
tableView.reloadRows(at: [indexPath], with: .top)
}
I'm assuming you want to have your M and F appear inside your pickerView. To do this you have to create a reference. This can be done through an IBOutlet or you can do it programmatically like so: let pickerView = UIPickerView(). On a side note, I am also assuming you are only using one pickerView in the View Controller. If you are using multiple, set each of the pickerViews a tag. This can be done in the interface builder and programatically like so: pickerView.tag = 1. Then change your pickerView functions based on those tags.
Create a variable.
var gender = "M"
Initialise again your gender variable. In this delegate method.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
gender = gender[row]
// Then reload your TableView row
table view.reloadData()
}
Add these lines to your cellforrow_atIndexPath.
Cell.genderIdicatorLbl.text = gender
Note: genderIndicatorLbl is IBOutlet for your gender indicator Label

extract value from UIpickerview as variable

so i have this code for a UIpickerview and i want to get the value selected as a variable to use later, i tried using a variable gravity but it says "initialisation of variable gravity was never used...." here's my code:
var g = ["9.807", "3.711"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return g.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return g[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var gravity = Float(g[row])
}
You need to declare the var outside of the scope of your method:
var gravity: Float?
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
gravity = Float(g[row])
}
A local variable scope is limited to the method and as you don't use it within the method you get a warning for that.
This is Swift not Javascript :)
Your gravity var is local, so the given warning makes total sense (i.e., its scope is limited to the given function).
You should use the following UIPickerView method to get the currently selected index:
func selectedRow(inComponent component: Int) -> Int
For instance:
let gIndex = pickerView.selectedRow(inComponent: 0)
let gValue = Float(g[gIndex])!
Note that you do not even need to implement the didSelectRow delegate method to get this information.
As correctly pointed out in the comments, you of course need to declare and bind your UIPickerView to an instance var of your view controller:
#IBOutlet weak var pickerView: UIPickerView!
To use pickerView, first mention its DataSource and Delegate.
UIPickerViewDataSource, UIPickerViewDelegate
Now create this by right clicking on pickerView -> to the ViewContoller.swfit.You will get like:
#IBOutlet var pickerView: [UIPickerView]!
Create an array which contains the content you want to show in pickerView. Just like this:
var array:[String] = ["David","Mikel","Akhil","Akshay","Zlatan"]
Now you have to use pickerView functions. These functions can only be used if you mention DataSource and Delegate in class. The functions are:
// number of rows or number of components
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// number of rows that we have to set
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return array.count
}
// set title in picker view according to the elements
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return array[row]
}
// when an element is selected, store in a string
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerString = array[row]
print(pickerString) // pickerString is String type. You can create this, to show the content selected.
}

Resources