Skip to content

Commit

Permalink
Merge pull request #17 from AndrewZub/gigalol
Browse files Browse the repository at this point in the history
change deprecated method
  • Loading branch information
bendodson authored Mar 22, 2021
2 parents 034200e + 12cbf85 commit 563477f
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions MonthYearPickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,40 @@ import UIKit

class MonthYearPickerView: UIPickerView, UIPickerViewDelegate, UIPickerViewDataSource {

var months: [String]!
var years: [Int]!
var months = [String]()
var years = [Int]()

var month = Calendar.current.component(.month, from: Date()) {
didSet {
selectRow(month-1, inComponent: 0, animated: false)
selectRow(month - 1, inComponent: 0, animated: false)
}
}

var year = Calendar.current.component(.year, from: Date()) {
didSet {
selectRow(years.index(of: year)!, inComponent: 1, animated: true)
if let firstYearIndex = years.firstIndex(of: year) {
selectRow(firstYearIndex, inComponent: 1, animated: true)
}
}
}

var onDateSelected: ((_ month: Int, _ year: Int) -> Void)?

override init(frame: CGRect) {
super.init(frame: frame)
self.commonSetup()
commonSetup()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonSetup()
commonSetup()
}

func commonSetup() {
// population years
var years: [Int] = []
if years.count == 0 {
var year = NSCalendar(identifier: NSCalendar.Identifier.gregorian)!.component(.year, from: NSDate() as Date)
var year = Calendar(identifier: .gregorian).component(.year, from: Date())
for _ in 1...15 {
years.append(year)
year += 1
Expand All @@ -58,15 +60,14 @@ class MonthYearPickerView: UIPickerView, UIPickerViewDelegate, UIPickerViewDataS
}
self.months = months

self.delegate = self
self.dataSource = self
delegate = self
dataSource = self

let currentMonth = NSCalendar(identifier: NSCalendar.Identifier.gregorian)!.component(.month, from: NSDate() as Date)
self.selectRow(currentMonth - 1, inComponent: 0, animated: false)
let currentMonth = Calendar(identifier: .gregorian).component(.month, from: Date())
selectRow(currentMonth - 1, inComponent: 0, animated: false)
}

// Mark: UIPicker Delegate / Data Source

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
Expand Down Expand Up @@ -94,8 +95,8 @@ class MonthYearPickerView: UIPickerView, UIPickerViewDelegate, UIPickerViewDataS
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let month = self.selectedRow(inComponent: 0)+1
let year = years[self.selectedRow(inComponent: 1)]
let month = selectedRow(inComponent: 0) + 1
let year = years[selectedRow(inComponent: 1)]
if let block = onDateSelected {
block(month, year)
}
Expand Down

0 comments on commit 563477f

Please sign in to comment.