import UIKit
class SecondViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
//UI TextFields
@IBOutlet weak var ChassisText: UITextField!
@IBOutlet weak var YearFromText: UITextField!
@IBOutlet weak var YearToText: UITextField!
@IBOutlet weak var MakeText: UITextField!
@IBOutlet weak var ModelText: UITextField!
//Used Variables
var YearFrom:String = "0"
var YearTo:String = "2020"
var ChassisNumber:String = ""
var ArrayYear:[String] = [""]
var ArrayMake = [String]()
var ArrayModel = [String]()
var i:Int = 0
var holdSelect = 0
//UIPickerView
var pickerselection: UIPickerView!
//Boolean Variables
var checkYearFrom: Bool = false
var checkYearTo: Bool = false
var checkMake: Bool = false
var checkModel: Bool = false
//Picker Actions
func DonePicker()
{
if checkYearFrom {
YearFromText.text = "\(ArrayYear[holdSelect])"
YearFrom = "\(ArrayYear[holdSelect])"
YearFromText.resignFirstResponder()
checkYearFrom = false
}
else if checkYearTo {
YearToText.text = "\(ArrayYear[holdSelect])"
YearTo = "\(ArrayYear[holdSelect])"
YearToText.resignFirstResponder()
checkYearTo = false
}
}
func CancelPicker()
{
YearFromText.resignFirstResponder()
YearToText.resignFirstResponder()
ModelText.resignFirstResponder()
MakeText.resignFirstResponder()
}
//Picker View Functions
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
if checkYearFrom || checkYearTo {
return ArrayYear[row]
}
else
{
return ArrayYear[row]
}
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
if checkYearFrom || checkYearTo {
return ArrayYear.count
}
else
{
return ArrayYear.count
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
holdSelect = row
pickerView.reloadAllComponents()
}
}
I have the whole code above creating the picker view programmatically and also the cancel and done buttonItem.
This line of code is where I create an Year Array:
var YearFrom:String = "0"
var YearTo:String = "2020"
var ArrayYear:[String] = [""]
var i:Int = 0
On the viewDidLoad this is where the for loop is created for the year:
for var index = 1980; index <= 2016; index++ {
ArrayYear.insert("\(index)", atIndex: i)
}
i = 0
//Creating Picker Programatically
pickerselection = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300))
pickerselection.backgroundColor = .whiteColor()
pickerselection.showsSelectionIndicator = true
pickerselection.delegate = self
pickerselection.dataSource = self
In the running phase where I test the picker view on the 'YearFromText' the array that I created is not displayed (1980 - 2016).
Is there something wrong with the implementation of the ArrayYear.
I'm new to swift programming.
If you just want the years in an Array then you can simply do this:
let years = (1980...2016).map { String($0) }