How to Use UIDatePicker From UIKit in iPhone Programming

Matthew Campbell, October 15th

Here is a great way to ask your users to input dates on your iPhone app.

When you use UIDatePicker in your app you will a tumbler that lets users select a date that you may use in other parts of your app.

In order to use UIDatePicker you will need to implement the UIPickerViewDelegate delegate and then add a UIPicker to your subview. To retrieve the date that the user inputs you can use the target-action pattern to assign a method to execute once the user has picked a date. See the video below:

Here is the code from the example:

The first thing we needed to do was indicate that our view controller would be acting as a delegate for the UITextField. To do this you simply need to declare it in the interface file:

This part is straightforward enough: you essentially create a label and a date picker and add them both to the view controller’s subview collection. This is the typical pattern used to build up a view in code.

This is the method that you assigned to the UIControlEventValueChanged event above using the target-action pattern.

How this works is that when the user stops manipulating the tumbler an event called UIControlEventValueChanged occurs. Since we assigned the changeDateInLabel method as an action for this event the code in that method will execute updating the date in the label.

Learning how to program is 5% reading and 95% action so the best way to learn is to do it yourself. Create a simple iPhone app that uses the date picker code.

Let us know how it goes in the comments below!

Thanks for the article. Was wondering if you had any tips or links to slick ways to show and hide a date picker. Seems that it needs a “done” button if you want to dismiss it. Know of any guidelines to present it (ie. push on navigation controller, present as modal, just animate it in, etc.)? Thanks again.

@Julio – the easiest thing to do is to add a UIButton to your view and then assign an action to do that will make the data picker disappear. You can make the date picker disappear using this line of code:

datePicker.hidden = YES;

A more elegant solution is a little out of the scope of this article, but it essentially involves putting the data picker on an UIActionSheet. UIActionSheets slide in from the bottom and then slide out when you are done with them. If you search on this site you will see a video example of how to use the UIActionSheet. What you can do is add the date picker to the UIActionSheet’s subview collection (I have not tested this specifically but I am pretty sure that is the direction you would go in).

How to Use UIDatePicker From UIKit in iPhone Programming…

You’ve been kicked (a good thing) – Trackback from – iPhone SDK links, community driven…

  • Hours of videos, demos & screen casts
  • Detailed HANDS-ON Exercises
  • Much Much Much More
  • UIKit
  • Objective-C
  • Core Data
  • More!
  • Learn How To Make An iPhone App right now using my proven system.

    PS: Tons of us are having a blast living off our own code, join us now. Click here to get all the details.