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:

#import <uikit/UIKit.h>

@interface DatePickerViewController : UIViewController {
        
        IBOutlet UIDatePicker *datePicker;
}

@end

 

 

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.

- (void)viewDidLoad {
NSDate *now = [[NSDate alloc] init];
[datePicker setDate:now animated:YES];
[now release];
[super viewDidLoad];
}

 

 

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.

 

-(IBAction)buttonPressed:(id)sender{

NSDate *selected =[datePicker date];
NSString *message =[[NSString alloc] initWithFormat:
@"The Date and Time you selected is: %@",selected];
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:
@"Date and Time Selected"
message:message
delegate:nil
cancelButtonTitle:@"Yes, I did."
otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
}

Figure 1: Date Picker

You can Download SourceCode from here DatePicker

Leave a Comment:

7 comments
Bilal says May 13, 2010

Hi.
Thx for the useful help.
I need little more help, I need only date part in picker, by default it shows date and time. how can I do that??

Reply
Sushant says May 13, 2010

If you want only date part in the picker, then you have to change in the Interface Builder. Open the Interface Builder -> Open View window -> Select picker from the view window and bring up Attribute Inspector and change the Mode into Date only. After doing that,You can see only date will display in the view window.

Try this , if you have any problem then please let us know.

Reply
Loly says June 22, 2010

Hii
thank u 4 u answers
if this time picker can arise when start typing un text field?
how can i do that

i found no such picker in mode of keyboard :S

Reply
Loly11 says June 22, 2010

Hii

if this time picker can arise when start typing un text field?
how can i do that

i found no such picker in mode of keyboard :S

Reply
raj says February 22, 2011

thank u

Reply
Tribhuvan Nath Dwivedi says March 14, 2011

Thnx 4 this Tutorials..

Reply
you host in says October 10, 2012

You really make it appear so easy with your presentation but I to find this matter to be really something which I feel I’d by no means understand. It kind of feels too complex and extremely extensive for me. I’m having a look ahead in your next publish, I will try to get the hold of it!

Reply
Add Your Reply