3

first time posting here, so hope i am doing this right.

I am trying to create a picker or date picker that allows me to display only the years from 1900 to this year(2015).
so it should looking something like:

" Year house was built: picker of years between 1900-2015 "

The only way i can think to do this is with a Picker View and an array of 115 values and i don't believe this is the right way.

Any help or guidance would be greatly appreciated and apologies if i am using the website wrongly.

Wain
  • 118,658
  • 15
  • 128
  • 151
Matthew Ghent
  • 31
  • 1
  • 5
  • Did you look at using a date picker? – Wain Mar 23 '15 at 13:36
  • 1
    Yes i did, there are only four modes available they are, Date and Time, Time, Date, Count Down Timer. I need exclusively just the year. – Matthew Ghent Mar 23 '15 at 13:38
  • Why don't you think using a `UIPickerView` is right? Sounds pretty good to me. – Stuart Mar 23 '15 at 13:41
  • Because i'd need an array of 115 numbers, could i loop through a date range in Swift? – Matthew Ghent Mar 23 '15 at 13:45
  • P.S. http://stackoverflow.com/questions/12017791/uidatepicker-to-show-year-only – iAnurag Mar 23 '15 at 13:52
  • @MatthewGhent Yes, exactly. Just store your years as an array of `Int`s. Swift has a super neat syntax for iterating ranges: `for year in 1900...2015 { }`. – Stuart Mar 23 '15 at 13:58
  • @MatthewGhent Oh just remembered there's an even better syntax for this in Swift... arrays can be initialised with a range directly, so actually you can just write: `let years = Array(1900...2015)`. – Stuart Mar 23 '15 at 16:05

1 Answers1

2

Simply use a UIPickerView and a simple for-loop statement to build the years and fill the UIPickerView

for (var i=1899; i<2015; i++) 
array[i] = i + 1
r4id4
  • 5,877
  • 8
  • 46
  • 76