Populate UIPickerView with items from class - ios

I have the following class:
class JobItem {
var ItemID:String = String()
var Qty:Int = Int()
var Item:String = String()
}
I also have a collection of items in this class like so:
var jobitems: [JobItem] = []
I would like to populate my pickerview with just the Item part from each instance of the class in the array and I'm not sure how. I successfully filled it with data from a dummy array I created so it's working OK, just need that data in.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return jobitems.count
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return as? String
}

Try this in your titleForRow, you just need to return the correct Item string with the correct row that can mapped to index of your array:
return jobitems[row].Item

Just takeout element from array to your instance class & populate what you wants,
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String {
var titleFoRow: String? = nil
var classObject: ModelClass? = nil
classObject = self.arrayCountryList[row]
self.titleFoRow = classObject.instanceVarName
return self.titleFoRow
}

Related

I can't add a UIPickerview for gender in a registration form

I am creating a registration form and I have to include the gender inside a text field using a picker view. However, I am getting question marks instead of male and female.
Here is my code:
#IBOutlet weak var genderfield: UITextField!
let pickerView = UIPickerView()
let gender1 = ["Male", "Female"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return gender1.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return gender1[row]
}
func pickerView(_ pickerView:UIPickerView,didSelectRow row: Int,forComponent component: Int){
genderfield.text = gender1[row]
genderfield.resignFirstResponder()
}
And in ViewDidLoad:
pickerView.delegate = self
pickerView.dataSource = self
genderfield.inputView = pickerView
I don't think there is something wrong in my code. I have been googling for hours.
You get question marks because you have the wrong signature on your titleForRow delegate method.
Change:
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return gender1[row]
}
to:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return gender1[row]
}
Note the addition of _ just after the (.
You can not added UIPickerViewDelegate, UIPickerViewDataSource.
so when you add delegate and data source of picker view its work fine.
like this:
extension RegisterVC : UIPickerViewDelegate, UIPickerViewDataSource{
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return gender1.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return gender1[row]
}
func pickerView(_ pickerView:UIPickerView,didSelectRow row: Int,inComponent component: Int){
genderfield.text = gender1[row]
genderfield.resignFirstResponder()
}

How to connect pickerviews and labels in 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]
}
}

UIPickerView depending on first component

So I have a UIPickerView with two components. The first one called "Cupboards", the second "Drawer". They have a one to many relationships in CoreData Cupboards <-->> Drawer. I would like that if I select a Cupboard, only the matching Drawers get displayed. I'm not sure how to get that working with the relationship.
That's what I have for showing the Cupboard:
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if(component==0){
return cupboards.count
}
xxxx
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if(component==0){
return cupboards[row].name
}
xxxx
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if(component==0){
}
}
Thank you in advance
Update:
Got it like that now, but I get no results.
var cupboardDrawers = [Drawer]()
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if(component==0){
let request: NSFetchRequest<Drawer> = Drawer.fetchRequest()
request.predicate = NSPredicate(format: "cupboard == %#", cupboards[row].name!)
do{
let result = try mgdContext.fetch(request)
cupboardDrawers = result
addEatDataPicker.reloadAllComponents()
}catch{
print(error.localizedDescription)
}
}
}
You need to create and update a cupboardDrawers array with the fetching results:
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if(component==0){
return cupboards.count
} else {
return cupboardDrawers.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if(component==0){
return cupboards[row].name
} else {
return cupboardDrawers[row].yourProperty
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if(component==0){
// you can add this part to a separate function and call it here
cupboardDrawers = []
let request = NSFetchRequest<Drawer>(entityName:”Drawer”)
request.predicate = YourPredicate with cupboards[row].name
do {
let items = context.fetch(request)
guard items.count > 0 else {
print(“no items found”)
return
}
cupboardDrawers = items
//reload your picker
} catch {
print(error)
}
}
}

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
}

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