Using Date Pickers

Following a previous post at http://howto.oz-apps.com/2012/09/creating-picker-views-gideros-ios-only.html that illustrated how to set up an UIPicker view, a couple of users asked how they could set up and use an UIDatePicker.

The UIDatePicker is like an UIPicker but it does not inherit from it, which means that the way it works is different.

The Date Picker allows for selecting a couple of components, namely date, time, date and time and a countdown timer.

the simplest form of using an UIDatePicker is as follows
  local datePicker = UIDatePicker:init()
  getRootViewController():view():addSubview(datePicker)

you have a date picker on the screen. This can be positioned as you want by setting the frame using it as
  datePicker:setFrame(CGRect(0,264,0,0))
The picker is now positioned at the bottom of the screen instead of the top of the screen.


The date and time on the picker would reflect the current date and time that it was created. This can be changed as has been in the screenshot above. It shows the 1st of July, but first let us handle when the picker is changed.

To handle the picker change, we need a function that is called and we can set this function as follows
  function dateChanged()
    print("Changed")
  end

  picker:addTarget_action_forControlEvents(self, "dateChanged", UIControlEventValueChanged)
  picker:setDelegate(self)

Now every time we change a date time component, the message "Changed" is printed to the Output console. To display the selection from the picker, we can simply say
  print(picker:date())

This is of type userdata as it is a structure, not a Lua data type. Similarly to set the custom date or time for the picker, we need to provide it a NSDate with the appropriate date/time. We can create a NSDate type object from a NSDate component as follows
 comp = NSDateComponents:init()
  comp:setDay(1)
  comp:setMonth(7)
  comp:setYear(1980)
  comp:setHour(5)

Next we need to convert this component into a NSCalendar object as follows
  greg = NSCalendar:initWithCalendarIdentifier("gregorian")
  theDate = greg:dateFromComponents(comp)

we now have theDate which is a NSDate object and we can use this to set the UIDatePicker date/time accordingly.

NOTE: I believe this is a bug in the UIDatePicker where the date selected is one day in the past. It could be something to do with the time zones, but the bottom line is that though the display says July 1 1981 16:30 , the dateChanged function displays 1981-06-30 19:00:00 + 0000 understandable, this is in reference to +0000 where as I am in timezone +10:30.

The picker mode can be changed by using the function
  picker:setDatePickerMode(0) -- Default Mode
the possible values that can be used with this function are
0 - Default value
1 - Date Picker, displays Month|Day|Year
2 - DateAndTime Picker, displays DOW Month Day| Hour| Min| Sec| AM/PM
3 - Countdown timer, allows for picking a duration for countdown.

While the function picker:getDate() works for most situations, if you set up a countdown timer, this is not very helpful, the function that can be of use at this time is picker:countDownDuration()

That's all there is to using UIDatePickers, if you need to reference using NSDates, the Apple documentation is quite impressively complete and can be found at the Apple website https://developer.apple.com/library/ios/navigation/

if you get the wonderful application Dash from the Mac App Store then you can view the documentation offline in the app rather then online.


if other objects are required to create an UI, then a UIView can be created that will hold all of the objects. You can read up on the Apple site for more details.

Comments

Popular Posts