开发者

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 sho开发者_JAVA百科w 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];  
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜