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
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?
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 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
}}