How To Use Picker In iPhone?

We are using UIDatePicker in our app. So you have to use UIDatePicker you will need to implement the UIPickerViewDelegate delegate and then add a UIPicker to your subview.

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



Step 2: Now,double click .xib file and open Interface Builder. We need a date picker,so look for Date Picker in the Library,and drag one over to the View Window .Then Save close the nib and go back to xcode.

Step 3: In the ViewDidLoad,We create new NSDate object. An NSDate object created this way will hold the current date and time. We then set datePicker to that date,which ensure every time this view loads, the Picker will reset to the current date and time.



Step 4: Double click .xib file and open Interface Builder.  Grab Round Rect Button from the Library,and place it below the Date Picker.Double click it,and give it a title of “Select”. Save,close the nib, and go to the xcode.

Step 5: We added the implementation of button Pressed and we overrode viewDidLoad.


Figure 1: Date Picker

You can Download SourceCode from here DatePicker