I am new to Swift but I feel like I'm picking it up pretty well.
I have four UITextField with InputView's that display UIPickerViews, and I programatically placed them within the app. My code shows no errors, all UIPickerViews pop up (picker, picker2, picker3, picker4), but only the array from "climate" shows in all four views. So my obvious issue is... how in the world do I get the remaining arrays (environment, region, budget) to display in their respected UIPickerViews!
also, side request, how would I disable the text field so that no one could be able to paste anything into the text field
Here is my code:
import UIKit
class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
#IBOutlet weak var climateText: UITextField!
#IBOutlet weak var environmentText: UITextField!
#IBOutlet weak var regionText: UITextField!
#IBOutlet weak var budgetText: UITextField!
var picker = UIPickerView()
var picker2 = UIPickerView()
var picker3 = UIPickerView()
var picker4 = UIPickerView()
//MARK: Arrays for PickerViews
var climates = ["Hot","Warm","Cold","Anywhere"]
var environment = ["Beach","City","Mountain","Rural","Anywhere"]
var region = ["Central America","Carribean","Europe","North America","South America","Anywhere"]
var budget = ["$0-500","$501-1000","$1001-1500","$1501-2500","$2501 and up"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
picker.tag = 0
picker2.tag = 1
picker3.tag = 2
picker4.tag = 3
picker.delegate = self
picker2.delegate = self
picker3.delegate = self
picker4.delegate = self
picker.dataSource = self
picker2.dataSource = self
picker3.dataSource = self
picker4.dataSource = self
climateText.inputView = picker
environmentText.inputView = picker2
regionText.inputView = picker3
budgetText.inputView = picker4
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if picker.tag == 0 {
return climates.count
} else if picker2.tag == 1 {
return environment.count
} else if picker3.tag == 2 {
return region.count
} else if picker4.tag == 3 {
return budget.count
}
return 1
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if picker.tag == 0 {
return climates[row]
} else if picker2.tag == 1 {
return environment[row]
} else if picker3.tag == 2 {
return region[row]
} else if picker4.tag == 3 {
return budget[row]
}
return ""
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if picker.tag == 0 {
climateText.text = climates[row]
} else if picker2.tag == 1 {
environmentText.text = environment[row]
} else if picker3.tag == 2 {
regionText.text = region[row]
} else if picker4.tag == 3 {
budgetText.text = budget[row]
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I'm not a fan of using tags (see point 2 here), but to get you past this problem, change your didSelectRow function to do this instead:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 0 {
climateText.text = climates[row]
} else if pickerView.tag == 1 {
environmentText.text = environment[row]
} else if pickerView.tag == 2 {
regionText.text = region[row]
} else if pickerView.tag == 3 {
budgetText.text = budget[row]
}
}
Your problem simply was that you weren't looking to see the tag from the picker view the user just made a section with.
Related
Im trying to have two different pickerViews on the same viewController and let them represent different data. I have tried a different ways to do this, but both picker views showing same Data, but can't put it all together. Any help would be awesome!
here is my code:
#IBOutlet weak var Eventtype: UITextField!
let pickerView = UIPickerView()
var Eventstype = ["Birthday" , "Marriege" , "Get together", "Conference"]
#IBOutlet weak var datepickertxt: UITextField!
var datepicker = UIDatePicker()
#IBOutlet weak var Duration: UITextField!
let duration = UIPickerView()
var Durations = ["6-8","1-5","7-9","10-12"]
override func viewDidLoad() {
super.viewDidLoad()
createDatePicker()
pickerView.delegate = self
pickerView.dataSource = self
duration.delegate = self
duration.dataSource = self
Eventtype.inputView = pickerView
Duration.inputView = duration
// Do any additional setup after loading the view.
}
////For Event Picker view
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView.tag == 0) {
//pickerView1
return Eventstype.count
} else if (duration.tag == 1){
//pickerView2
return Durations.count
}
return 1
//return DurationTxt.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 0) {
return Eventstype[row]
} else if (duration.tag == 1){
return Durations[row]
}
return ""
}
//return DurationTxt[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView.tag == 0) {
Eventtype.text = Eventstype[row]
self.view.endEditing(false)
}else if (duration.tag == 1){
Duration.text = Durations[row]
self.view.endEditing(true)
}
}
#IBOutlet weak var Eventtype: UITextField!
let pickerView = UIPickerView()
var Eventstype = ["Birthday" , "Marriege" , "Get together", "Conference"]
#IBOutlet weak var datepickertxt: UITextField!
var datepicker = UIDatePicker()
#IBOutlet weak var Duration: UITextField!
let duration = UIPickerView()
var Durations = ["6-8","1-5","7-9","10-12"]
override func viewDidLoad() {
super.viewDidLoad()
createDatePicker()
pickerView.delegate = self
pickerView.dataSource = self
duration.delegate = self
duration.dataSource = self
Eventtype.inputView = pickerView
Duration.inputView = duration
pickerView.tag = 0
duration.tag = 1
// Do any additional setup after loading the view.
}
////For Event Picker view
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView.tag == 0) {
//pickerView1
return Eventstype.count
} else if (duration.tag == 1){
//pickerView2
return Durations.count
}
return 1
//return DurationTxt.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 0) {
return Eventstype[row]
} else if (duration.tag == 1){
return Durations[row]
}
return ""
}
//return DurationTxt[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView.tag == 0) {
Eventtype.text = Eventstype[row]
self.view.endEditing(false)
}else if (duration.tag == 1){
Duration.text = Durations[row]
self.view.endEditing(true)
}
}
This is done by tag or else yo can check in delegate like
if pickerView == duration
Set the tags for pickerview in viewDidLoad as shown below,
pickerView.tag = 0
duration.tag = 1
class ViewController: UIViewController {
#IBOutlet var txtDuration: UITextField!
#IBOutlet var txtEventType: UITextField!
var pickerView = UIPickerView()
var arrEventType : [String] = []
var arrDuration : [String] = []
var currentTextField : UITextField?
override func viewDidLoad() {
super.viewDidLoad()
arrEventType = ["Birthday" , "Marriage" , "Get together", "Conference"]
arrDuration = ["6-8","1-5","7-9","10-12"]
}
}
extension ViewController : UIPickerViewDelegate , UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if currentTextField == txtEventType {
return arrEventType.count
} else if currentTextField == txtDuration {
return arrDuration.count
} else {
return 0
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if currentTextField == txtEventType {
return arrEventType[row]
} else if currentTextField == txtDuration {
return arrDuration[row]
} else {
return ""
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if currentTextField == txtEventType {
txtEventType.text = arrEventType[row]
} else if currentTextField == txtDuration {
txtDuration.text = arrDuration[row]
}
}
}
extension ViewController : UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
self.pickerView.delegate = self
self.pickerView.dataSource = self
currentTextField = textField
if currentTextField == txtEventType {
currentTextField?.inputView = pickerView
} else if currentTextField == txtDuration {
currentTextField?.inputView = pickerView
}
}
}
Above lines of code may solve your issue with just one UIPickerView and without tags
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
}}
I'm taking my first baby steps in Swift and I want to make a conversor app.
I want to use 2 pickerviews, one for choose the origin type and the other to choose the destination type.
The problem is that I can't make the 2 pickerviews work... What I want is click in one textfield and show the first pickerview and then click in other textfield and show the other pickerview but what is happening is that it always opening the first pickerview.
Here's the code:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
#IBOutlet weak var txtOrigem: UITextField!
#IBOutlet weak var txtDestino: UITextField!
#IBOutlet weak var pickerpeso: UIPickerView! = UIPickerView()
#IBOutlet weak var pickerpeso2: UIPickerView! = UIPickerView()
var pesos = ["Escolher Opção","Gramas", "Quilogramas", "Toneladas", "Libras", "Onças"]
var pesos2 = ["ola","Gramas", "Quilogramas", "Toneladas", "Libras", "Onças"]
override func viewDidLoad() {
super.viewDidLoad()
pickerpeso.delegate = self
pickerpeso2.delegate = self
txtOrigem.delegate = self
txtDestino.delegate = self
pickerpeso.tag = 0
pickerpeso2.tag = 1
pickerpeso.hidden = true;
pickerpeso2.hidden = true;
txtOrigem.text = pesos[0]
txtDestino.text = pesos2[0]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 0 {
return pesos.count
}
else if pickerView.tag == 1 {
return pesos2.count
}
return 1
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 0 {
return pesos[row]
}
else if pickerView.tag == 1 {
return pesos2[row]
}
return ""
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 0 {
txtOrigem.text = pesos[row]
}
else if pickerView.tag == 1 {
txtDestino.text = pesos2[row]
}
pickerpeso.hidden = true
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
pickerpeso.hidden = false
return false
}
}
Hope someone could help me. Sorry for my poor English.
Thanks in advance!
The problem is your textFieldShouldBeginEditing method. You are just setting the .hidden property for one of the pickerViews. Add tags to both of your textFields (like 2 and 3) and change the method to this:
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if textField.tag == 2 {
pickerpeso.hidden = false
pickerpeso2.hidden = true
} else if textField.tag == 3 {
pickerpeso.hidden = true
pickerpeso2.hidden = false
}
return false
}
Dependent on which textField will get the focus one pickerView is hidden and the other one is visible.