I have been trying to figure out this problem for hours, now I have only one small problem remaining:
I use 2 UI pickers on one view controller, and when I try to select one (aka I'm clicking into the textfield) all I see is question marks, but if I click on one of them, the text will appear in the textfield. I just don't see what am I choosing in the picker.
I already tried using normal pickerviews, I used tags for each pickers, but nothing seemed to work. I know how it should work, I watched a ton of tutorials, but something is still missing. Can you please help me? Thank you!
Here is the code:
import UIKit
class SelectionViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
#IBOutlet weak var ageTextField: UITextField!
#IBOutlet weak var weightTextField: UITextField!
#IBOutlet weak var genderTextField: UITextField!
#IBOutlet weak var lifeStyleTextField: UITextField!
let picker1 = UIPickerView()
let picker2 = UIPickerView()
var genders = ["Male", "Female"]
var lifeStyle = ["Sitting", "Normal", "Active"]
override func viewDidLoad() {
super.viewDidLoad()
picker1.dataSource = self
picker1.delegate = self
picker2.dataSource = self
picker2.delegate = self
genderTextField.inputView = picker1
lifeStyleTextField.inputView = picker2
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView : UIPickerView!) -> Int{
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if pickerView == picker1 {
return genders.count
} else {
return lifeStyle.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if pickerView == picker1 {
return genders[row]
} else if pickerView == picker2{
return lifeStyle[row]
}
else {
print("No pickerview selected.")
}
return ("Pickeview not selected")
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == picker1 {
genderTextField.text = genders[row]
self.view.endEditing(false)
} else if pickerView == picker2{
lifeStyleTextField.text = lifeStyle[row]
self.view.endEditing(false)
}
else {
print("Love love love I want your love")
}
}
}
And here's the thing I see:
You need this signature ( missed _ )
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
and numberOfComponentsInPickerView should return 1
I looked on here for an answer, but do not believe this question has been answered. I have multiple text boxes that use a couple different pickers. I would like for the first text box the user selects the material, and then based on the material selected they can only see the type of material that corresponds to it. Below in my code when I am naming my arrays you can see that it says typeBrassData or typeCopperData so if the user selects Brass or Copper above only the options for Brass show, does anyone know how to do this? Below is the code I was using when it was only 2 picker views. Thanks all in advance!
import UIKit
class MetalCalculatorViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var materialField: UITextField!
#IBOutlet weak var typeField: UITextField!
#IBOutlet weak var volumeField: UITextField!
#IBOutlet weak var quantityField: UITextField!
var pickerMaterial = UIPickerView()
var pickerType = UIPickerView()
var materialData = ["Brass","Copper","CuNiSn", "Cupro-Nickel","High Performance Alloys", "Leaded Brass","Nickel Silvers","Phosphor Bronze","Tin Brass"]
var typeData = ["C101", "C102","C1092","C110","C122","C14415","C151", "C155","C18070", "C18080", "C19020", "C19025", "C19210","C194", "C195", "C197", "C1972", "C210","C220", "C226", "C230", "C240", "C260","C268","C272", "C350", "C353", "C422", "C425", "C4252", "C510","C511", "C5118", "C519","C521", "C638", "C654", "655", "C688", "C7025", "C70250", "C7026", "C7035", "C706", "C710", "C715", "C7250", "C752", "C757","C764","C770", "XP5", "XP10", "XP55", "XP125", "XP150", "XP175", "MAX251C", "MSP1"]
var typeBrassData = ["C210","C220", "C226", "C230", "C240", "C260","C268","C272"]
var typeHighPerformanceData = ["C14415","C151", "C155","C18070", "C18080", "C19020", "C19025", "C19210","C194", "C195", "C197", "C1972", "C638", "C654", "655", "C688", "C7025", "C70250", "C7026", "C7035", "XP5", "XP10", "XP55", "XP125", "XP150", "XP175", "MAX251C", "MSP1"]
var typeCopperData = ["C101", "C102","C1092","C110","C122"]
var typeCuproNickelData = ["C706", "C710", "C715"]
var typeLeadedBrassData = ["C350", "C353"]
var typeTinBrassData = ["C422", "C425", "C4252"]
var typePhosphorBronzeData = ["C510","C511", "C5118", "C519","C521"]
var typeCunNiSnData = ["C725"]
var typeNickelSilverData = ["C752", "C757","C764","C770"]
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLoad() {
super.viewDidLoad()
pickerMaterial.delegate = self
pickerMaterial.dataSource = self
pickerMaterial.tag = 1
materialField.inputView = pickerMaterial
pickerType.delegate = self
pickerType.dataSource = self
pickerType.tag = 2
typeField.inputView = pickerType
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MetalCalculatorViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
// Do any additional setup after loading the view.
}
func dismissKeyboard() {
view.endEditing(true)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == pickerMaterial {
return materialData.count
} else if pickerView == pickerType{
return typeData.count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == pickerMaterial {
return materialData[row]
} else if pickerView == pickerType{
return typeData[row]
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == pickerMaterial {
materialField.text = materialData[row]
} else if pickerView == pickerType{
typeField.text = typeData[row]
}
}
You can use the dictionary data structure to set the values of the pickerviews
var materialSelected = ""
var typeArr = [String]()
//I have done for only three materials obviously you can extend it...
var dict = ["Brass" : ["C210","C220", "C226", "C230", "C240", "C260","C268","C272"],
"Copper" : ["C101", "C102","C1092","C110","C122"], "CuNiSn" : ["C725"]] as [String: Any]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == pickerMaterial {
return materialData.count
} else if pickerView == pickerType{
typeArr = dict[materialSelected] as! [String]
return typeArr.count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == pickerMaterial {
return materialData[row]
} else if pickerView == pickerType{
typeArr = dict[materialSelected] as! [String]
return typeArr[row]
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == pickerMaterial {
materialSelected = materialData[row]
materialField.text = materialSelected
} else if pickerView == pickerType{
typeArr = dict[materialSelected] as! [String]
typeField.text = typeArr[row]
}
}
I am very new to swift Xcode and would like some help on UIPickerView.
I am trying to create two UIPickerViews, one called "From" and one called "To". "From" contains A, B and "To" contains B, C
Basically, if "From" is A and "To" is B then a Photo of car will appear on the same viewController.
if "From" is B and "To" is B then a photo of strawberry will appear and so on.
Can anyone give me any hints on how to do this? I've only managed to create the first UIPickerView.
import UIKit
class ViewController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet var Picker1:UIPickerView!
var Array = ["A","B","C"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib
Picker1.delegate = self
Picker1.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return Array[row]
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return Array.count
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
}
var savedVal1 = "" as String
var savedVal2 = "" as String
var array1 = NSArray()
var array2 = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
Picker1.delegate = self
Picker1.dataSource = self
Picker1.tag = 1
Picker2.delegate = self
Picker2.dataSource = self
Picker2.tag = 2
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 1 {
return array1[row]
}
else{
return array2[row]
}
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 1 {
return array1.count
}
else{
return array2.count
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){
if pickerView.tag == 1 {
savedVal1 = array1[row]
// compare 2 values here & then take steps which fulfill your criteria.
}
else{
savedVal2 = array2[row]
//compare 2 values here & then take steps which fulfill your criteria.
}
}
Hope it helps.
Where is your PickerView2?
#IBOutlet var Picker2:UIPickerView!
In viewdidload load as you done
Picker1.delegate = self
Picker1.dataSource = self
If you are using same data source or different please check the picker view in UIPickerViewDelegate, UIPickerViewDataSource method like
If (Picker1) {
} else if (Picker2) {
}
How to navigate between two viewcontrollers using a UIPickerview
I am trying to navigate between two viewcontrollers using a UIPickerview, so i first learned how to use didSelectRow to change the title of a label and then i tried using some code to navigate to another viewcontroller but that didn't work.
The code that i used was:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet var label: UILabel!
#IBOutlet var pickerView: UIPickerView!
var food = ["hello", "hi", "hey"]
var placementAnswer = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pickerView.delegate = self
pickerView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return food.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return food[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
placementAnswer = row
}
#IBAction func labelChanged(sender: UIButton) {
if (placementAnswer == 0){
}else if (placementAnswer == 1){
let view2 =
self.storyboard?.instantiateViewControllerWithIdentifier("view2") as! ViewController2
self.navigationController?.pushViewController(view2, animated: true)
}else if (placementAnswer == 2){
}
}
}
I am trying to navigate between two viewcontrollers using a UIPickerview, so i first learned how to use didSelectRow to change the title of a label and then i tried using some code to navigate to another viewcontroller but that didn't work.
The code that i used was:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet var label: UILabel!
#IBOutlet var pickerView: UIPickerView!
var food = ["hello", "hi", "hey"]
var placementAnswer = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pickerView.delegate = self
pickerView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return food.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return food[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
placementAnswer = row
}
#IBAction func labelChanged(sender: UIButton) {
if (placementAnswer == 0){
}else if (placementAnswer == 1){
let view2 = self.storyboard?.instantiateViewControllerWithIdentifier("view2") as! ViewController2
self.navigationController?.pushViewController(view2, animated: true)
}else if (placementAnswer == 2){
}
}
}
As paranoidcoder pointed out I'm not sure your labelChanged method is getting called, which could be the problem with your existing code.
I'm assuming you want to change the view controller after a certain row is selected in the picker view. Putting the if statement to change views in didSelectRow would make the most sense in that case.
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
placementAnswer = row // unnecessary variable
if (placementAnswer == 0){}
else if (placementAnswer == 1)
{
let view2 = self.storyboard?.instantiateViewControllerWithIdentifier("view2") as! ViewController2
self.navigationController?.pushViewController(view2, animated: true)
}
else if (placementAnswer == 2) {}
}