Problem in Datepicker and alertsheet - iphone

I am implementing UIDATEPICKER. The problem is that when i click on the button, The alert sheet is appear and datepicker is add as subview but i want two button,
1-cancel 2-done
what will i do?
UIActionSheet *aac = [[UIActionSheet alloc] initWithTitle:#"How many?"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
aac.actionSheetStyle = UIActionSheetStyleBlackOpaque;
sheet = aac;
UIDatePicker *theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
if(IsDateSelected==YES)
{
theDatePicker.datePickerMode = UIDatePickerModeDate;
theDatePicker.maximumDate=[NSDate date];
}else {
theDatePicker.datePickerMode = UIDatePickerModeTime;
}
self.dtpicker = theDatePicker;
[theDatePicker release];
[dtpicker addTarget:self action:#selector(dateChanged) forControlEvents:UIControlEventValueChanged];
pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(DatePickerCancelClick)];
[barItems addObject:cancelBtn];
////
it doesnot work ..I want this....
what will i do?

-(IBAction)DatePickerView
{
iRolegameAppDelegate *appDelegate = (iRolegameAppDelegate *)[[UIApplication sharedApplication]delegate];
pickerViewDate = [[UIActionSheet alloc] initWithTitle:#"How many?"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
theDatePicker.datePickerMode = UIDatePickerModeDateAndTime;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]autorelease]];
[dateFormatter setDateFormat:#"MM/dd/YY h:mm a"];
[theDatePicker addTarget:self action:#selector(dateChanged) forControlEvents:UIControlEventValueChanged];
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle=UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(DatePickerDoneClick)];
[barItems addObject:flexSpace];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
[barItems addObject:spacer];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(DatePickerCancelClick)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:theDatePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)];
NSDateFormatter *currentdateformate = [[NSDateFormatter alloc] init];
[currentdateformate setDateFormat:#"HH:MM, EEEE, dd-MMMM-yyyy"];
appDelegate.timestamp1 = [currentdateformate stringFromDate:[theDatePicker date]];
NSDateFormatter *currentdateformate1 = [[NSDateFormatter alloc] init];
[currentdateformate1 setDateFormat:#"MMM dd, yyyy HH:mm"];
self.updatedate = [currentdateformate1 stringFromDate:[theDatePicker date]];
}
-(IBAction)dateChanged{
iRolegameAppDelegate *appDelegate = (iRolegameAppDelegate *)[[UIApplication sharedApplication]delegate];
NSDateFormatter *currentdateformate = [[NSDateFormatter alloc] init];
[currentdateformate setDateFormat:#"HH:MM, EEEE, dd-MMMM-yyyy"];
appDelegate.timestamp1 = [currentdateformate stringFromDate:[theDatePicker date]];
NSLog(#"%#",appDelegate.timestamp1);
[currentdateformate setDateFormat:#"MMM dd, yyyy HH:mm"];
self.updatedate = [currentdateformate stringFromDate:[theDatePicker date]];
}
-(void)DatePickerCancelClick
{
self.pickerViewDate.hidden = YES;
self.view.hidden = NO;
[self.pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];
}
-(BOOL)closeDatePicker:(id)sender{
//iRolegameAppDelegate *appDelegate = (iRolegameAppDelegate *)[[UIApplication sharedApplication]delegate];
[pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];
[pickerToolbar release];
[pickerViewDate release];
//[SelectedTextField resignFirstResponder];
if([ self.updatedate isEqualToString:#"nil"]){
NSDateFormatter *currentdateformate = [[NSDateFormatter alloc] init];
[currentdateformate setDateFormat:#"MMM dd, yyyy HH:mm"];
self.updatedate = [currentdateformate stringFromDate:[theDatePicker date]];
[dateSelectButton setTitle:self.updatedate forState:UIControlStateNormal];
self.updatedate = #"";
}
else{
[dateSelectButton setTitle:self.updatedate forState:UIControlStateNormal];
self.updatedate = #"";
}
return YES;
}
-(IBAction)DatePickerDoneClick{
[self closeDatePicker:self];
}

maybe you should get rid of the UIActionSheet and do it like I did for another question.
UIDatePicker pop up after UIButton is pressed

Related

IOS actionsheet button selection not working properly

Hi I am developing small IOS application in which I am using actionsheet. So my problem is like this when I clicked outside the actionsheet it is closing my actionsheet it is calling - (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex method even though I am not clicking any button of actionsheet. It is returning lastindexof action sheet button. So how to avoid this. Any solution? I tried with -(void)actionSheet:(UIActionSheet *)action didDismissWithButtonIndex:(NSInteger)buttonIndex
But still same result.
I write the time picker by my self instead UIActionSheet in iOS8:
date = [NSDate date];
timePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
timePicker.datePickerMode = UIDatePickerModeDateAndTime;
timePicker.hidden = NO;
timePicker.date = date;
displayFormatter = [[NSDateFormatter alloc] init];
[displayFormatter setTimeZone:[NSTimeZone localTimeZone]];
[displayFormatter setDateFormat:#"MM月dd日 EEE HH:mm"];
formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
startDisplayTimeString = [displayFormatter stringFromDate:timePicker.date];
startTimeString = [formatter stringFromDate:timePicker.date];
NSTimeInterval interval = 24*60*60*1;
NSDate *endDate = [[NSDate alloc] initWithTimeIntervalSinceNow:interval];
endDisplayTimeString = [displayFormatter stringFromDate:endDate];
endTimeString = [formatter stringFromDate:endDate];
[_startTimeLabel setText:startDisplayTimeString];
[_endTimeLabel setText:endDisplayTimeString];
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
pickerToolbar.tintColor = [UIColor whiteColor];
[pickerToolbar sizeToFit];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelBtnPressed:)];
[cancelBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:253.0/255.0 green:68.0/255.0 blue:142.0/255.0 alpha:1.0],
NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *titleButton;
float pickerMarginHeight = 168;
titleButton = [[UIBarButtonItem alloc] initWithTitle:#"title" style:UIBarButtonItemStylePlain target: nil action: nil];
[titleButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:253.0/255.0 green:68.0/255.0 blue:142.0/255.0 alpha:1.0],
NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleDone target:self action:#selector(setTimePicker)];
[doneBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:253.0/255.0 green:68.0/255.0 blue:142.0/255.0 alpha:1.0],
NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
NSArray *itemArray = [[NSArray alloc] initWithObjects:cancelBtn, flexSpace, titleButton, flexSpace, doneBtn, nil];
[pickerToolbar setItems:itemArray animated:YES];
if(iPad){
[pickerToolbar setFrame:CGRectMake(0, 0, 320, 44)];
UIViewController* popoverContent = [[UIViewController alloc] init];
popoverContent.preferredContentSize = CGSizeMake(320, 216);
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
popoverView.backgroundColor = [UIColor whiteColor];
[popoverView addSubview:timePicker];
[popoverView addSubview:pickerToolbar];
popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController presentPopoverFromRect:CGRectMake(0, pickerMarginHeight, 320, 216) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}else{
timeBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-300, 320, 246)];
[timeBackgroundView setBackgroundColor:[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0]];
[timeBackgroundView addSubview:pickerToolbar];
[timeBackgroundView addSubview:timePicker];
[self.view addSubview:timeBackgroundView];}
When you tap outside of the UIActionSheet, UIKit performs the same action as if you had tapped the cancel button. The buttonIndex that is sent in - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex is the cancel button index. There isn't a way to know if the user tapped outside of your UIActionSheet, by default. If you want this functionality you will have to code your own custom action sheet or handle the outside tap as a cancel tap.

how can i go back after selecting date in date picker

I am facing a problem in date picker. when am i clicking on date picker done button. then my code is crashing.
-(IBAction)btn:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
datelabel.text = [NSString stringWithFormat:#"%#",
[df stringFromDate:[NSDate date]]];
[df release];
[self.view addSubview:datelabel];
[datelabel release];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
[datePicker addTarget:self
action:#selector(LabelChange:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
[datePicker release];
}
- (void)LabelChange:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
datelabel.text = [NSString stringWithFormat:#"%#",
[df stringFromDate:datePicker.date]];
}
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackOpaque;
toolbar.frame=CGRectMake(0,4,datePicker.frame.size.width,40);
[datePicker addSubview:toolbar];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(DoneSelected)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpace,doneBtn,nil]];
[flexibleSpace release];
}
-(void)DoneSelected
{
your implementation code
}

UIDatePicker in UIActionSheet giving trouble while updating date on a label

I am new to IOS development. I am developing an app which requires a form to be filled by the user for the user details. The details includes a Date of Birth Field for which I am using UIDatePicker. I am adding those UIDatePicker and UIActionSheet programatically and wanted to update the date on a label. The dates are getting updated however they are getting overwritten on same label.
Here is the code
-(IBAction)dateButtonClicked:(id)sender {
actionSheet = [[UIActionSheet alloc] initWithTitle:#"Choose Date"delegate:self cancelButtonTitle:#"cancel" destructiveButtonTitle:nil otherButtonTitles:#"Select",nil];
[actionSheet showInView:self.view ];
[actionSheet setFrame:CGRectMake(0, 117, 320, 383)];
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
[datePickerView setMinuteInterval:5];
[datePickerView setTag: kDatePickerTag];
[actionSheet addSubview:datePickerView];
datelabel = [[UILabel alloc] init];
datelabel.frame = CGRectMake(55, 92, 300, 50);
datelabel.backgroundColor = [UIColor clearColor];
datelabel.textColor = [UIColor blackColor];
datelabel.font = [UIFont fontWithName:#"Verdana-Bold" size: 12.0];
datelabel.textAlignment = UITextAlignmentCenter;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: #"MM/dd/YYYY h:mm a"];
datelabel.text = [NSString stringWithFormat:#"%#",
[formatter stringFromDate:[NSDate date]]];
[self.view addSubview:datelabel];
NSArray *subviews = [actionSheet subviews];
[[subviews objectAtIndex:SelectButtonIndex] setFrame:CGRectMake(20, 266, 280, 46)];
[[subviews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 317, 280, 46)];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex]) {
[datePickerView addTarget:self
action:#selector(LabelChange:)
forControlEvents:UIControlEventValueChanged];
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Date" message:msg delegate:nil cancelButtonTitle: #"Dismiss" otherButtonTitles:nil];
// [alert show];
}
}
- (void)LabelChange:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
datelabel.text = [NSString stringWithFormat:#""];
datelabel.text = [NSString stringWithFormat:#"%#",
[df stringFromDate:datePickerView.date]];
}
Please let me know the changes needs to be done in order to void such problem.
This code works for me like a charm !!!
in .h file
#import <UIKit/UIKit.h>
#interface EXPViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate, UIActionSheetDelegate>{
UIActionSheet *pickerViewPopup;
UIDatePicker *pickerView;
IBOutlet UILabel *dateLabel;
}
- (IBAction)dateButtonClicked:(id)sender;
#end
In .m file:
-(IBAction)dateButtonClicked:(id)sender{
[self showPickerView];
}
-(void)showPickerView {
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
pickerView = nil;
pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
((UIDatePicker*)pickerView).datePickerMode = UIDatePickerModeDate;
((UIDatePicker*)pickerView).date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0, 0, 320, 464)];
}
-(void)doneButtonPressed:(id)sender {
//Do something here here with the value selected using [pickerView date] to get that value
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd, MMM YYYY"];
NSString* dateString = [dateFormat stringFromDate:((UIDatePicker*)pickerView).date];
yourLabel.text = dateString;
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}
-(void)cancelButtonPressed:(id)sender{
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}
This is because whenever your dateButtonClicked action is fire , datePickerView and datelabel allocating again and again, so just do one thing, put your code of datePickerView and datelabel code outside dateButtonClicked clicked , you can put in viewDIdLoad
You can implement delegate method of picker,
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}

date picker in ios application

i am new to iPhone development. I want to add a UIDatePicker when textfield is selected.
two date picker is there.
- (void)textFieldDidBeginEditing:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
}
I used this but got same value for the two different datepickers. I want that if I press textfield1 then datepicker opens and saves that value in textfield one and same for the second textfield . Thanks.
#Maulik:
First thing - (void)textFieldDidBeginEditing:(UITextField *)aTextField is called every time the textfield is selected for editing.
So for both your selection i.e. textfield1 and textfield2 same code is going to be called.
What you can do is at the tap of done button, Check which textfield has opened up the picker and save the value in that textfield.
What I meant is take a textfield in .h file
UITextField *selectedTextField;
Then in textFieldDidBeginEditing method set it as selectedTextField = aTextField;
and at the tap of done button i.e in doneButtonPressed set the selected value for selectedTextField.text. And thats it.. you are done :)
I have an easy solution.
Set delegate <UIPickerViewDelegate,UIPickerViewDataSource>
In .h file:
UIDatePicker *dtPicker;
UIActionSheet *pickerViewPopup;
-(IBAction)dt_picker:(id)sender;
In .m file:
-(IBAction)dt_picker:(id)sender{
pickerViewPopup = [[UIActionSheet alloc] init];
const CGFloat toolbarHeight = 44.0f;
dtPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, toolbarHeight, 0, 0)];
NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
// if (dateStatus==0) {
dtPicker.datePickerMode = UIDatePickerModeDate;
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
dtPicker.minimumDate = [NSDate date];
/* }else{
datePicker.datePickerMode = UIDatePickerModeTime;
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"HH:mm:ss"];
}*/
dtPicker.hidden = NO;
dtPicker.date = [NSDate date];
[dtPicker addTarget:self action:#selector(LabelChange:) forControlEvents:UIControlEventValueChanged];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, toolbarHeight)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(btncancelPressed:)];
[barItems addObject:btnCancel];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
lbl1.text=[NSString stringWithFormat:#"%#",dtPicker.date];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:dtPicker];
[pickerViewPopup showInView:self.view.superview];
[pickerViewPopup setBounds:CGRectMake(0,0,self.view.frame.size.width, 464)];
}
-(void)LabelChange:(id)sender{
NSDateFormatter* formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[formatter1 setDateFormat:#"yyyy-MM-dd"];
NSString *dateString= [formatter1 stringFromDate:dtPicker.date];
lbl1.text=dateString;
}
-(void)btncancelPressed:(id)sender{
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}
-(void)doneButtonPressed:(id)sender{
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}

Problem selecting date in UIDatePicker iphone

I´m using the UIDatePicker in a form but the problem is that when I select the date and time, the time in the text field is 5 hours after the time showed in the picker. I've read that there's a bug in date picker but I don't know how to solve this. I need to show the time of Mexico. I've tried doing this but nothing change.
datePicker.calendar = [NSCalendar autoupdatingCurrentCalendar];
datePicker.timeZone = [NSTimeZone localTimeZone];
datePicker.locale = [NSLocale currentLocale];
Can anyone help me with this??? XD
Thank you guys!!
If you aren't using it already, I'd suggest doing:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterMediumStyle];
[df setTimeStyle:NSDateFormatterMediumStyle];
NSString *stringToDisplay = [df stringFromDate:myDateObject];
NSDateFormatter should take care of any time zone issues for you. You can read more here.
o this: this will help you
//Date Picker
(void)textFieldDidBeginEditing:(UITextField *)aTextField {
[aTextField resignFirstResponder];
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
}
(void)doneButtonPressed:(id)sender{
//Do something here here with the value selected using [pickerView date] to get that value
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}
(void)cancelButtonPressed:(id)sender{
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}