I'm trying to figure out why my spinner won't hold integers. When I run the program, question marks appear in the spinner instead of 1,2,3,4 and so on all the way up to 100. I'm new to Swift and this is my very first time working with the spinner/Picker. My program is supposed to calculate Miles per Gallon, but that can't happen if I can't get the spinner to work, lol.
Here is the code that sets up the variables and the spinner:
class SecondViewController:
UIViewController,UIPickerViewDataSource,UIPickerViewDelegate
{
var BlopSoundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Blop", ofType: "mp3")!)
var soundAudioPlayer = AVAudioPlayer()
var miles = 0.0
var gallonsUsed = 0.0
var mpg = 0.0
// Variables for slider
var minMiles = 1
var maxMiles = 1000
@IBOutlet weak var milesDrivenLabel: UILabel!
@IBAction func milesDrivenSlider(sender: UISlider)
{
}
@IBOutlet weak var gallonsUsedPicker: UIPickerView!
let pickerData = [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,26,27,28,29,30,31,
32,33,34,35,36,37,38,39,40,41,
42,43,44,45,46,47,48,49,50,51,
52,53,54,55,56,57,58,59,60,61,
62,63,64,65,66,67,68,69,70,71,
72,73,74,75,76,77,78,79,80,81,
82,83,84,85,86,87,88,89,90,91,
92,93,94,95,96,97,98,99,100]
There is no code for the submit button yet, but here is the rest of the code for the spinner and the program:
@IBAction func submitButton(sender: UIButton)
{
soundAudioPlayer.play()
let row = gallonsUsedPicker.selectedRowInComponent(0)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> Int!
{
return pickerData[row]
}
override func viewDidLoad()
{
super.viewDidLoad()
gallonsUsedPicker.dataSource = self
gallonsUsedPicker.delegate = self
soundAudioPlayer = AVAudioPlayer(contentsOfURL: BlopSoundURL, error: nil)
// Do any additional setup after loading the view, typically from a nib.
} // End of viewDidLoad
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} // End of didReceiveMemoryWarning
} // End of SecondViewController
I'm really not sure what the code in the functions do because the book didn't do a very good job of explaining it. Then, I'll have to figure out how to take the number from the spinner and store it in a variable, so that I can do the mpg calculation. I know how to do it with a slider, but not a spinner. Is it basically the same thing? I have a feeling that this is going to get interesting. Thank you, your help is much appreciated.