please forgive my ignorance. I am learning from near scratch.
I'm trying to populate multiple pickerView instances based on the selection of a pickerView at the top of the view. I want the choices shown in the remaining pickerviews to depend on what item has been selected in the first pickerview.
I have 6 pickerViews in total. the 1st makes the general selection I want to base the remainder on.
Pickerview1 has 4 components. If the user selects component 4, all remaining pickerViews to show choices. If the user selects component 2 or 3, I want pickerView 4 and 5 to display N/A while the others show choices. If the user selects component 1, I want pickerViews 3-5 to display N/A while 1-2 show choices.
I assume to do this, I would need the index value of the selected component in pickerView1 to make an if else statement for the func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent etc.
I found selectedRow(inComponent:) in the documentation but have not been able to find working examples of how to implement it in my case.
Attached are some snips of my code so far:
import UIKit
class FirstViewController: UIViewController, UIPickerViewDelegate,
UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
var capacities = ["6","9","12","15","18","24"]
var notUsed = "N/A"
var oduList = ["MXZ-2C20", "MXZ-3C24", "MXZ-3C30", "MXZ-5C42"]
var result:Float = 0.0
var one4num:Float = 1.0
var factor:Float = 1.08
var per5ft:Float = 5
var factory:Float = 98
var selectedRow = 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
selectedRow = pickerView.selectedRow(inComponent: index)
if pickerView == oduPicker {
return oduList.count
}
else if pickerView == idu1PickerView {
return capacities.count
}
else if pickerView == idu2PickerView {
return capacities.count
}
else if pickerView == idu3PickerView {
return capacities.count
}
else if pickerView == idu4PickerView {
return capacities.count
}
else {
return capacities.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == oduPicker {
return oduList[row]
}
else if pickerView == idu1PickerView {
return capacities[row]
}
else if pickerView == idu2PickerView {
return capacities[row]
}
else {
return capacities[row]
}
}
#IBOutlet weak var oduPicker: UIPickerView!
#IBOutlet weak var idu1PickerView: UIPickerView!
#IBOutlet weak var idu2PickerView: UIPickerView!
#IBOutlet weak var idu3PickerView: UIPickerView!
#IBOutlet weak var idu4PickerView: UIPickerView!
#IBOutlet weak var idu5PickerView: UIPickerView!
#IBOutlet weak var one4Label: UILabel!
#IBOutlet weak var one4Slider: UISlider!
#IBOutlet weak var output: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.oduPicker.delegate = self
self.oduPicker.dataSource = self
self.idu1PickerView.delegate = self
self.idu1PickerView.dataSource = self
self.idu2PickerView.delegate = self
self.idu2PickerView.dataSource = self
self.idu3PickerView.delegate = self
self.idu3PickerView.dataSource = self
}
#IBAction func one4Slider(_ sender: Any) {
one4Label.text = "\(one4Slider.value)"
}
#IBAction func three8Slider(_ sender: Any) {
three8Label.text = "\(three8Slider.value)"
}
#IBAction func calculateButton(_ sender: Any) {
result = factor*(one4num-factory)/per5ft
output.text = "\(result)"
}
}
If I implement the suggestion this way:
import UIKit
class FirstViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
var capacities = ["6","9","12","15","18","24"]
var notUsed = ["N/A"]
var oduList = ["MXZ-2C20", "MXZ-3C24", "MXZ-3C30", "MXZ-5C42"]
var result:Float = 0.0
var one4num:Float = 1.0
var factor:Float = 1.08
var per5ft:Float = 5
var factory:Float = 98
var rowSelected: Int?
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == oduPicker{
self.rowSelected = row
// make anything
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == oduPicker {
return oduList.count
}
else if pickerView == idu1PickerView {
return capacities.count
}
else if pickerView == idu2PickerView {
return capacities.count
}
else if pickerView == idu3PickerView {
if rowSelected! < 2 {
return 1
}
else {
return capacities.count
}
}
else if pickerView == idu4PickerView {
if rowSelected! < 3 {
return 1
}
else {
return capacities.count
}
}
else {
if rowSelected! < 4 {
return 1
}
else {
return capacities.count
}
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == oduPicker {
return oduList[row]
}
else if pickerView == idu1PickerView {
return capacities[row]
}
else if pickerView == idu2PickerView {
return capacities[row]
}
else if pickerView == idu3PickerView {
if rowSelected! < 2 {
return notUsed[row]
}
else {
return capacities[row]
}
}
else if pickerView == idu4PickerView {
if rowSelected! < 3 {
return notUsed[row]
}
else {
return capacities[row]
}
}
else if pickerView == idu5PickerView {
if rowSelected! < 4 {
return notUsed[row]
}
else {
return capacities[row]
}
}
else {
return capacities[row]
}
}
#IBOutlet weak var oduPicker: UIPickerView!
#IBOutlet weak var idu1PickerView: UIPickerView!
#IBOutlet weak var idu2PickerView: UIPickerView!
#IBOutlet weak var idu3PickerView: UIPickerView!
#IBOutlet weak var idu4PickerView: UIPickerView!
#IBOutlet weak var idu5PickerView: UIPickerView!
#IBOutlet weak var one4Label: UILabel!
#IBOutlet weak var one4Slider: UISlider!
#IBOutlet weak var output: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.oduPicker.delegate = self
self.oduPicker.dataSource = self
self.idu1PickerView.delegate = self
self.idu1PickerView.dataSource = self
self.idu2PickerView.delegate = self
self.idu2PickerView.dataSource = self
self.idu3PickerView.delegate = self
self.idu3PickerView.dataSource = self
self.idu4PickerView.delegate = self
self.idu4PickerView.dataSource = self
self.idu5PickerView.delegate = self
self.idu5PickerView.dataSource = self
}
#IBAction func one4Slider(_ sender: Any) {
one4Label.text = "\(one4Slider.value)"
}
#IBAction func calculateButton(_ sender: Any) {
result = factor*(one4num-factory)/per5ft
output.text = "\(result)"
}
}
The code breaks the "FirstViewController" and seems to invalidate the "numberOfRows" and "titleForRows" statements as well as a cascade of errors below that.
If i implement after those two functions:
else {
return capacities[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == oduPicker{
self.rowSelected = row
// make anything
}
#IBOutlet weak var oduPicker: UIPickerView!
#IBOutlet weak var idu1PickerView: UIPickerView!
#IBOutlet weak var idu2PickerView: UIPickerView!
#IBOutlet weak var idu3PickerView: UIPickerView!
#IBOutlet weak var idu4PickerView: UIPickerView!
#IBOutlet weak var idu5PickerView: UIPickerView!
It breaks all the pickerView variables like oduPicker etc.
If I try to bring the statement into the 1st function as such:
var rowSelected: Int?
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, numberOfRowsInComponent component: Int) -> Int {
if pickerView == oduPicker {
self.rowSelected = row
return oduList.count
}
else if pickerView == idu1PickerView {
return capacities.count
}...
It again breaks the "FirstViewController" with the error "Type 'FirstViewController' does not conform to protocol 'UIPickerViewDataSource'"
I'm also forced to put a "!" after "rowSelected" for wrapping/unwrapping. I'm not sure if what this means or if it's important at the moment.
Update:
If I implement per Samuel's suggestion as such:
import UIKit
class FirstViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
var capacities = ["6","9","12","15","18","24"]
var notUsed = ["N/A"]
var oduList = ["MXZ-2C20", "MXZ-3C24", "MXZ-3C30", "MXZ-5C42"]
var result:Float = 0.0
var one4num:Float = 1.0
var factor:Float = 1.08
var per5ft:Float = 5
var factory:Float = 98
var selectedRow = 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
selectedRow = pickerView.selectedRow(inComponent: index)
if pickerView == oduPicker {
return oduList.count
}
else if pickerView == idu1PickerView {
return capacities.count
}
else if pickerView == idu2PickerView {
return capacities.count
}
else if pickerView == idu3PickerView {
if selectedRow < 2 {
return 1
}
else {
return capacities.count
}
}
else if pickerView == idu4PickerView {
if selectedRow < 3 {
return 1
}
else {
return capacities.count
}
}
else {
if selectedRow < 4 {
return 1
}
else {
return capacities.count
}
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == oduPicker {
return oduList[row]
}
else if pickerView == idu1PickerView {
return capacities[row]
}
else if pickerView == idu2PickerView {
return capacities[row]
}
else if pickerView == idu3PickerView {
if selectedRow < 2 {
return notUsed[row]
}
else {
return capacities[row]
}
}
else if pickerView == idu4PickerView {
if selectedRow < 3 {
return notUsed[row]
}
else {
return capacities[row]
}
}
else if pickerView == idu5PickerView {
if selectedRow < 4 {
return notUsed[row]
}
else {
return capacities[row]
}
}
else {
return capacities[row]
}
}
#IBOutlet weak var oduPicker: UIPickerView!
#IBOutlet weak var idu1PickerView: UIPickerView!
#IBOutlet weak var idu2PickerView: UIPickerView!
#IBOutlet weak var idu3PickerView: UIPickerView!
#IBOutlet weak var idu4PickerView: UIPickerView!
#IBOutlet weak var idu5PickerView: UIPickerView!
#IBOutlet weak var one4Label: UILabel!
#IBOutlet weak var one4Slider: UISlider!
#IBOutlet weak var output: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.oduPicker.delegate = self
self.oduPicker.dataSource = self
self.idu1PickerView.delegate = self
self.idu1PickerView.dataSource = self
self.idu2PickerView.delegate = self
self.idu2PickerView.dataSource = self
self.idu3PickerView.delegate = self
self.idu3PickerView.dataSource = self
self.idu4PickerView.delegate = self
self.idu4PickerView.dataSource = self
self.idu5PickerView.delegate = self
self.idu5PickerView.dataSource = self
}
#IBAction func one4Slider(_ sender: Any) {
one4Label.text = "\(one4Slider.value)"
}
#IBAction func calculateButton(_ sender: Any) {
result = factor*(one4num-factory)/per5ft
output.text = "\(result)"
}
}
I get an error after the line:
selectedRow = pickerView.selectedRow(inComponent: index)
"Cannot convert value of type '(Any) -> Int' to expected argument type 'Int'"
You can use the delegate's methods. The method below can help you. The row parameter specific the row selected by user. You can try:
var rowSelected : Int?
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == idu1PickerView {
self.rowSelected = row
// make anything
}
...
}
If you have just one component you can use the method like you wrote:
var selectedRow = pickerView.selectedRow(inComponent: 0)
Related
UIPickerView shows data but does not transfer to UITextField.
i've checked other post to compare my code, still cannot find a solution. your help is greatly appreciated.
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {
#IBOutlet weak var hospNameField: UITextField!
#IBOutlet weak var teamNameField: UITextField!
#IBOutlet weak var mdNameField: UITextField!
#IBOutlet weak var selectionsPickerView: UIPickerView!
var hosp = ["hosp1", "hosp2", "hosp3"]
var team = ["team1", "team2", "team3"]
var mds = ["MD1", "MD2", "MD3"]
var itemSelected = ""
var textFieldSelected = UITextField()
weak var pickerView: UIPickerView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let pickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self
hospNameField.inputView = pickerView
teamNameField.inputView = pickerView
mdNameField.inputView = pickerView
hospNameField.delegate = self
teamNameField.delegate = self
mdNameField.delegate = self
self.pickerView = pickerView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func textFieldDidBeginEditing(_ textField: UITextField) {
textFieldSelected = textField
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if hospNameField.isFirstResponder {
return hosp.count
} else if teamNameField.isFirstResponder {
return team.count
} else if mdNameField.isFirstResponder {
return mds.count
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if hospNameField.isFirstResponder {
return hosp[row]
} else if teamNameField.isFirstResponder {
return team[row]
} else if mdNameField.isFirstResponder {
return mds[row]
}
return nil
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if teamNameField.isFirstResponder {
let itemselected = hosp[row]
hospNameField.text = itemselected
} else if teamNameField.isFirstResponder {
let itemselected = team[row]
teamNameField.text = itemselected
} else if mdNameField.isFirstResponder {
let itemselected = mds[row]
mdNameField.text = itemselected
}
}
}
You have forgotten to subscribe the delegate and data sources to the PickerView..
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {
#IBOutlet weak var hospNameField: UITextField!
#IBOutlet weak var teamNameField: UITextField!
#IBOutlet weak var mdNameField: UITextField!
#IBOutlet weak var selectionsPickerView: UIPickerView!
var hosp = ["hosp1", "hosp2", "hosp3"]
var team = ["team1", "team2", "team3"]
var mds = ["MD1", "MD2", "MD3"]
var itemSelected = ""
var textFieldSelected = UITextField()
weak var pickerView: UIPickerView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let pickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self
hospNameField.inputView = pickerView
teamNameField.inputView = pickerView
mdNameField.inputView = pickerView
hospNameField.delegate = self
teamNameField.delegate = self
mdNameField.delegate = self
self.pickerView = pickerView
/**
the below lines are mandatory for
pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
to execute
*/
self.pickerView?.delegate = self
self.pickerView?.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func textFieldDidBeginEditing(_ textField: UITextField) {
textFieldSelected = textField
}
// add this delegate and reload here
func textFieldShouldBeginEditing(_ textField: UITextField) ->
Bool {
self.pickerView?.reloadAllComponents()
return true
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if hospNameField.isFirstResponder {
return hosp.count
} else if teamNameField.isFirstResponder {
return team.count
} else if mdNameField.isFirstResponder {
return mds.count
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if hospNameField.isFirstResponder {
return hosp[row]
} else if teamNameField.isFirstResponder {
return team[row]
} else if mdNameField.isFirstResponder {
return mds[row]
}
return nil
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
/**
type here, need to check the responder for hospNameField also
you have executed teamNameField.isFirstResponder twice
*/
if hospNameField.isFirstResponder {
let itemselected = hosp[row]
hospNameField.text = itemselected
} else if teamNameField.isFirstResponder {
let itemselected = team[row]
teamNameField.text = itemselected
} else if mdNameField.isFirstResponder {
let itemselected = mds[row]
mdNameField.text = itemselected
}
}
}
I think your code is wrong in didSelectRow. check the first and second if, both run the same condition.
if teamNameField.isFirstResponder {
let itemselected = hosp[row]
hospNameField.text = itemselected
} else if teamNameField.isFirstResponder {
let itemselected = team[row]
teamNameField.text = itemselected
} else if mdNameField.isFirstResponder {
let itemselected = mds[row]
mdNameField.text = itemselected
}
I think you should change the first if to if hospNameField.isFirstResponder {..}
I have a trouble with signal SIGABRT, but I think that everything is ok.
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var vysledek: UILabel!
#IBOutlet weak var cisloJedna: UITextField!
#IBOutlet weak var cisloDva: UITextField!
#IBOutlet weak var znamenkoVyber: UIPickerView!
var array = ["*", "/", "+", "-"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
znamenkoVyber.delegate = self
znamenkoVyber.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return array.count;
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return array[row]
}
#IBAction func vypocitej(_ sender: Any) {
if array == ["*"] {
let vypocet = String(Int(cisloJedna.text!)! * Int(cisloDva.text!)!)
vysledek.text = vypocet
}
else if array == ["/"] {
let vypocet = String(Int(cisloJedna.text!)! / Int(cisloDva.text!)!)
vysledek.text = vypocet
}
else if array == ["+"] {
let vypocet = String(Int(cisloJedna.text!)! + Int(cisloDva.text!)!)
vysledek.text = vypocet
}
else if array == ["-"] {
let vypocet = String(Int(cisloJedna.text!)! - Int(cisloDva.text!)!)
vysledek.text = vypocet
}
}
}
Where can the problem be?
My pickerview is not displaying for some reason even after reloading all components. I click start and it just does not load up the pickerview as I want this to load and then you can select an option and go to the next question?
import UIKit
class QuestionsViewController: UIViewController, UIPickerViewDelegate {
#IBOutlet weak var Next: UIButton!
#IBOutlet weak var pickerview: UIPickerView!
#IBOutlet weak var itemLabel: UILabel!
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var Question: UILabel!
#IBAction func Next(_ sender: Any) {
cQuestion.currentQuestion = cQuestion.currentQuestion + 1
pickerview.reloadAllComponents()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//var LabelText: String = ""
//var arrayOfQuestions: [String] = ["&","&&","||","None of above"]
//var correctAns:String = ""
//var currentQuestion = 0
let cQuestion = Questions()
//init(arrayOfQuestions:String, correctAns:String, LabelText:String) {
//self.arrayOfQuestions = [arrayOfQuestions]
// self.correctAns = correctAns
// self.LabelText = LabelText
//}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
itemLabel.text = cQuestion.arrayOfQuestions[0]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if(cQuestion.currentQuestion == 0) {
Question.text = "Q1. Which is a logical OR operator"
return cQuestion.arrayOfQuestions.count
} else if (cQuestion.currentQuestion == 1) {
Question.text = "Q2. Compiler generates_file"
return cQuestion.arrayOfQuestions.count
}
hide()
Question.text = "You have finished"
Next.isHidden = true
return cQuestion.arrayOfQuestions.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
if(cQuestion.currentQuestion == 0) {
return cQuestion.arrayOfQuestions[row]
} else if (cQuestion.currentQuestion == 1) {
return cQuestion.arrayOfQuestions[row]
}
return cQuestion.arrayOfQuestions[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
if (cQuestion.currentQuestion == 0) {
let itemSelected = cQuestion.arrayOfQuestions[row]
itemLabel.text = itemSelected
} else if (cQuestion.currentQuestion == 1) {
let itemSelected = cQuestion.arrayOfQuestions[row]
itemLabel.text = itemSelected
}
}
func hide() {
pickerview.isHidden = true
itemLabel.isHidden = true
}
}
My guess: it looks like as soon as numberOfRowsInComponent is called (which it does as soon as it prepares to be added to the view) you also call hide() which in turns seems to set your pickerView to isHidden = true. I don't see anywhere in your code where isHidden is subsequently set to false.
If that happens to not be the case, also check what the value of pickerview.dataSource is to make sure it's not nil.
I am having an error in the "pickerview.reloadAllComponents()" and I cannot get into the 2nd question as there is an error which states
fatal error: unexpectedly found nil while unwrapping an Optional value
import UIKit
class QuestionsViewController: UIViewController, UIPickerViewDelegate {
#IBOutlet weak var Next: UIButton!
#IBOutlet weak var itemLabel: UILabel!
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var Question: UILabel!
#IBOutlet weak var pickerview: UIPickerView!
let cQuestion = Questions()
public var q1: [String] = ["&","&&","||","None of above"]
public var q2: [String] = ["&","&&","||","hello"]
#IBAction func NextAction(_ sender: Any){
cQuestion.currentQuestion = cQuestion.currentQuestion + 1
pickerview.reloadAllComponents()
}
override func viewDidLoad() {
label1.text = cQuestion.LabelText
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
itemLabel.text = cQuestion.q1[0]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
var qcount: Int = 0
if(cQuestion.currentQuestion == 0) {
Question.text = "Q1. Which is a logical OR operator"
qcount = cQuestion.q1.count
} else if (cQuestion.currentQuestion == 1) {
Question.text = "Q2. Compiler generates_file"
qcount = cQuestion.q2.count
} else {
hide()
Question.text = "You have finished"
Next.isHidden = true
}
//if(currentQuestion == 0) {
//Question.text = "Q1. Which is a logical OR operator"
//return q1.count
//} else if (currentQuestion == 1) {
//Question.text = "Q2. Compiler generates_file"
//return q2.count
//}
//hide()
//Question.text = "You have finished"
//Next.isHidden = true
//return q1.count
return qcount
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
if(cQuestion.currentQuestion == 0) {
return q1[row]
} else if (cQuestion.currentQuestion == 1) {
return q2[row]
}
return q1[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
if (cQuestion.currentQuestion == 0) {
let itemSelected = q1[row]
itemLabel.text = itemSelected
} else if (cQuestion.currentQuestion == 1) {
let itemSelected = q2[row]
itemLabel.text = itemSelected
}
}
func hide() {
pickerview.isHidden = true
itemLabel.isHidden = true
}
}
I think you have IBOutlet of pickerview is not connected properly, Make sure you need to check in Storyboard or Xib pickerview is connected properly or not?
i hope it will work for you
I have multiple UIPickerViews in a single ViewController. I believe I've set datasources and delegates properly, but, when I run the code, the PickerViews are not populating. What I am getting is "()" displaying in all the PickerViews.
Anyone have ANY idea what the heck I'm doing wrong? Everyplace I've looked/googled intimates I have this set up correctly.
XCode:
Simulator:
class GroundViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
let actionRatings = ["0","1","2","3","4","5"]
let hogLevels = ["0","1","2","3","4"]
let defTerrainTitles = ["Open", "Close", "Very Close", "Extr. CLose"]
var attackerAR = 0
#IBOutlet var attARPicker: UIPickerView!
#IBOutlet var attCombatFactorsInput: UITextField!
#IBOutlet var bonzaiLabel: UILabel!
#IBOutlet var bonzaiSwitch: UISwitch!
#IBOutlet var overrunSwitch: UISwitch!
#IBOutlet var defARPicker: UIPickerView!
#IBOutlet var defCombatFactorsInput: UITextField!
#IBOutlet var defHogLevel: UIPickerView!
#IBOutlet var defTerrain: UIPickerView!
#IBOutlet var lblSurpiseResults: UILabel!
#IBOutlet var lblAttackerResults: UILabel!
#IBOutlet var lblDefenderResults: UILabel!
#IBOutlet var lblShowDice: UILabel!
#IBAction func btnResolveCombat(sender: UIButton) {
//collect data from fields and output results
}
#IBAction func btnRest(sender: UIButton) {
//reset all components
}
override func viewDidLoad() {
super.viewDidLoad()
//set delegates & datasources
attARPicker.dataSource = self
attARPicker.delegate = self
defARPicker.dataSource = self
defARPicker.delegate = self
defTerrain.dataSource = self
defTerrain.delegate = self
defHogLevel.dataSource = self
defHogLevel.delegate = self
//check appSettings
bonzaiSwitch.setOn(false, animated: true)
if burmaSetting == false {
bonzaiLabel.hidden = true
bonzaiSwitch.hidden = true
}
if diceSetting == false {
lblShowDice.hidden = true
}
// 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.
}
//MARK: - Delegates and data sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
//MARK: Data Sources
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//set per pickerView.tag
/*
tag 1 = attARPicker
tag 2 = defARPicker
tag 3 = defHogLevel
tag 4 = defTerrain
*/
if (pickerView.tag == 1){
return actionRatings.count
} else if (pickerView.tag == 2) {
return actionRatings.count
} else if (pickerView.tag == 3){
return hogLevels.count
} else {
return defTerrainTitles.count
}
/*
switch pickerView.tag {
case 1, 2:
return actionRatings.count
case 3:
return hogLevels.count
case 4:
return defTerrainTitles.count
default:
return 0
}
*/
}
//MARK: Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//set per pickerview.tag
/*func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if (pickerView.tag == 1){
return "\(picker1Options[row])"
}else{
return "\(picker2Options[row])"
}*/
if (pickerView.tag == 1){
return actionRatings[row]
} else if (pickerView.tag == 2) {
return actionRatings[row]
} else if (pickerView.tag == 3){
return hogLevels[row]
} else {
return defTerrainTitles[row]
}
}
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let titleData = actionRatings[row]
let myTitle = NSAttributedString(string: String(titleData), attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 12.0)!,NSForegroundColorAttributeName:UIColor.blueColor()])
return myTitle
}}