Read Time: 2 min, 31 sec

Dec 27, 2012, Posted by: adminnoppl

I ran into this issue when I was programming for an iPhone app three days back… Issue is not able to set the date properly when a user picks a date in UIDatePicker . First, what is a UIDatePicker and how we can we create a UIDatePicker? UIDatePicker is used to pick the date or time in the app. // Creation of UIDatePicker . UIDatePicker *dateobj = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,0, 250, 250)]; // In the above code, we created UIDatePicker object as (datePicker) and allocated using (alloc) similar to UILabel and UIButton. We need to set the frame too. dateobj.datePickerMode = UIDatePickerModeDate; dateobj.datePickerMode = UIDatePickerModeTime; dateobj.datePickerMode = UIDatePickerModeDateAndTime; dateobj.datePickerMode = UIDatePickerModeCountDownTimer; From the above four lines of code, use any one of them for datePickerMode. 1. First line of code is for displaying Date alone. 2. Second line of code is for displaying Time alone. 3. Third line of code is for displaying both DateAndTime. 4. Fourth line of code is for displaying CountDownTimer. To show the date picker in a view, set hidden to NO. dateobj.hidden = NO; Below code is used to show date whenever picker gets created. = [NSDate date]; [NSDate date] –This specifies the current date (i.e. today’s date) For object (dateobj) i.e. for UIDatePicker, we are setting target, action ,forControlEvents. [dateobj addTarget:self action:@selector(LabelChange:) forControlEvents:UIControlEventValueChanged]; In some cases, whenever you select a date it won’t be displayed in a label or button. Reason for that is: you have to set .. ▪ forControlEvents:UIControlEventValueChanged ▪ action:@selector(LabelChange:) this method will be called (LabelChange:) - (void)LabelChange:(id)sender { NSLog(@”successfully created picker”); } You can set range of dates for example (this month/date/week to another) using below code. dateobj.minimumDate=[NSDate date]; // Starting date is minimumDate, here we set the starting date to (today’s date). NSTimeInterval obj =24*60*60; (1 day duration in 86400 seconds) Here using NSTimeInterval, we specify the duration between particular days. NSTimeInterval obj = hours*minutes*seconds; The object has to be in seconds (obj) NSDate *tomorrowdate=[NSDate dateWithTimeIntervalSinceNow:obj]; The object has to be in seconds (obj) dateobj.maximumDate=tomorrowdate; Entire code what I explained till now…. - (void)viewDidLoad { [super viewDidLoad]; UIDatePicker * dateobj = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 250, 250)]; dateobj.datePickerMode = UIDatePickerModeTime; dateobj.minimumDate=[NSDate date]; NSTimeInterval obj=24*60*60*; NSDate *tomorrowdate=[NSDate dateWithTimeIntervalSinceNow:obj]; dateobj.maximumDate=tomorrowdate; dateobj.hidden = NO; = [NSDate date]; [dateobj addTarget:self action:@selector(LabelChange:) forControlEvents:UIControlEventValueChanged]; Adding the datepicker to view. [self.view addSubview:dateobj]; } - (void)LabelChange:(id)sender { NSLog(@”successfully created picker”); } Issue is duration is set from today to tomorrow using the below code: NSTimeInterval obj=24*60*60;(1 day duration in 86400 seconds) So, we can only select dates of today and tomorrow. 1 day -NSTimeInterval obj=24*60*60; 1 week-NSTimeInterval obj=7*24*60*60; 1 month-NSTimeInterval obj=30(0r)31*24*60*60;

Contact Our Mobile Developers

UIDatePicker-iPhone App Programming

Rest Of The Best