Trying to populate a picker view

Hi Everyone,

I was wondering if I could have some help please?

I’m trying to populate a pickerview with numbers from 1 to my integer count (number of questions in my plist).

I’ve got this so far

import Foundation

import UIKit

class PickerView: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

var Category: String!
var numbDict: String!
var QuestionNumber: String!
var cell: UITableViewCell?
var currentTopicIndex = Int()
var setCount = Int()
var setNumber = Int()
var pickerString2 = [String]()
var selectedQuestionNumber: String!
var contacts: NSArray?
var dictContacts = Int()
var data = [String]()
var pickerArray: NSArray?



var competitionArray:NSMutableArray = []


@IBAction func backBtn(_ sender: AnyObject) {
    navigationController?.popToRootViewController(animated: true)
}

@IBOutlet weak var questionNumberPicker: UIPickerView!
@IBOutlet weak var numberOfRow: UILabel!

@IBAction func checkoutlet2(_ sender: AnyObject) {
    
    NSLog ("\(self.pickerString2)")
    NSLog ("\(self.Category)")
    
    
    
    NSLog ("\(self.selectedQuestionNumber)")
    
}











override func viewDidLoad() {
    super.viewDidLoad()
     print(currentTopicIndex)
    //  UIApplication.sharedApplication().statusBarStyle = .LightContent

// self.navigationController!.navigationBar.isHidden = true
self.questionNumberPicker.delegate = self
self.questionNumberPicker.dataSource = self
setCount = Quiz.quizzes[currentTopicIndex].plist.count
numbDict = String(setCount)
pickerArray = Array(1…setCount) as NSArray

    let pickerString2 = pickerArray
    print(pickerArray)
    
    
    
   
    print(pickerString2)
    
    self.questionNumberPicker?.reloadAllComponents()
    
    
    self.questionNumberPicker = UIPickerView()

// self.numberOfRow.text = self.pickerString2[0]

    print(self.pickerString2)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if(segue.identifier == "questionToQuiz"){
        
        //   Get the new view controller using [segue destinationViewController].
        let detailScene = segue.destination as! QuizController
        
        
        detailScene.numberOfQuestions = numberOfRow.text as String!
        
        
        
    }
}


func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerString2.count
    
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    
    
    return pickerString2[row]
}



func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    let selectedRow = pickerString2[row]
    numberOfRow.text = selectedRow
    selectedQuestionNumber = pickerString2[row]
    
}


// Do any additional setup after loading the view.

}

but it keeps coming up with a pickerString2 of 0. Can anyone see why this is?

I would appreciate any help you can offer. Many thanks in advance

ok I’ve managed to get the string for pickerString2 right. It now shows [“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “11”, “12”, “13”, “14”, “15”, “16”, “17”, “18”, “19”, “20”, “21”, “22”, “23”, “24”, “25”].

But for some reason the numbers are not showing in my picker view, and now its saying “fatal error: Index out of range” when I click the screen.

Can anyone see anything wrong with my code?

I would appreciate any help on the matter.

import Foundation
import UIKit

class PickerView: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

var Category: String!
var numbDict: String!
var QuestionNumber: String!
var cell: UITableViewCell?
var currentTopicIndex = Int()
var setCount = Int()
var setNumber = Int()
var pickerString2 = [String]()
var selectedQuestionNumber: String!
var contacts: NSArray?
var dictContacts = Int()

// var data = String
var pickerArray: NSArray?
var pickerData: [String] = String

var competitionArray:NSMutableArray = []


@IBAction func backBtn(_ sender: AnyObject) {
    navigationController?.popToRootViewController(animated: true)
}

@IBOutlet weak var questionNumberPicker: UIPickerView!
@IBOutlet weak var numberOfRow: UILabel!

@IBAction func checkoutlet2(_ sender: AnyObject) {
    
    NSLog ("\(self.pickerString2)")
    NSLog ("\(self.Category)")
    
    
    
    NSLog ("\(self.selectedQuestionNumber)")
    
}











override func viewDidLoad() {
    super.viewDidLoad()
     print(currentTopicIndex)
    //  UIApplication.sharedApplication().statusBarStyle = .LightContent

// self.navigationController!.navigationBar.isHidden = true
self.questionNumberPicker.delegate = self
self.questionNumberPicker.dataSource = self
setCount = Quiz.quizzes[currentTopicIndex].plist.count

    let min = 1
    let max = setCount
    let intArray: [Int] = Array(min...max)
    print(intArray)

// numbDict = String(setCount)
// pickerArray = Array(1…setCount) as NSArray

    let pickerString2 = intArray.map
    {
        String($0)
    }
    print(pickerArray)
    
    
    
   
    print(pickerString2)
    print(pickerString2.count)
    
    
    self.questionNumberPicker?.reloadAllComponents()
    
    
    self.questionNumberPicker = UIPickerView()
    
  self.numberOfRow.text = "0"

// self.pickerString2[0]

    print(self.pickerString2)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if(segue.identifier == "questionToQuiz"){
        
        //   Get the new view controller using [segue destinationViewController].
        let detailScene = segue.destination as! QuizController
        
        
        detailScene.numberOfQuestions = numberOfRow.text as String!
        
        
        
    }
}


func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerString2.count
    
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    
    
    return pickerString2[row]
}



func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    let selectedRow = pickerString2[row]
    numberOfRow.text = selectedRow
    selectedQuestionNumber = pickerString2[row]
    
}


// Do any additional setup after loading the view.

}

This topic was automatically closed after 166 days. New replies are no longer allowed.