iPhone:日期选择器 dd/mm/yyyy

5

我正在使用一个DatePicker,当用户点击一个textField时弹出。然而,选择器首先显示月份,而不是日期,并且这不是英国书写日期的方式。 有没有办法在DatePicker中设置日期格式为英国方式,例如dd/mm/yyyy? 我正在使用Achtionsheet:

-(IBAction)acsheet:(id)sender
{

    actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                              delegate:nil
                                     cancelButtonTitle:nil
                                destructiveButtonTitle:nil
                                     otherButtonTitles:nil];
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    //CGRect pickerFrame = CGRectMake(0, 45, 0, 0);
    pickerView1 = [[UIDatePicker alloc] init];
    pickerView1.datePickerMode = UIDatePickerModeDate;  

    [actionSheet addSubview:pickerView1];

    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet:)                    
                      forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];
    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];    
    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

    [pickerView1 addTarget:self
                    action:@selector(updateLabel:)
          forControlEvents:UIControlEventValueChanged]; 

    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self  action:@selector(dismissDatePicker:)] ;
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
    toolBar.tag = 11;
    toolBar.barStyle = UIBarStyleBlackTranslucent;

    [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
    [self.view addSubview:toolBar];

}
3个回答

17

UIDatePicker 默认使用 [NSLocale currentLocale]

UIDatePickerlocale 属性已经在iOS 5 中被弃用,但我认为您可以采取以下方法:

NSCalendar *cal = [NSCalendar currentCalendar];
cal.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
myDatePicker.calendar = cal;

这个应该可以运行。如果不能,请提交错误报告,我会修复它。:)


2
谢谢Dave,但出于某些原因这并不起作用。还有其他人遇到这个问题吗?我在这里看到了BBog的答案,看起来相当令人沮丧:http://stackoverflow.com/questions/9638698/ios-change-language-for-uidatepicker-by-locale - Stavash

7

UIDatePicker旨在适应用户的设置。使用locale:方法更改其格式是可能的,但在iOS 5.0中已被弃用。

您可以在iPhone的设置中查看您的格式:

设置 > 通用 > 国际设置 > 区域格式

如果您想要特定的格式,请考虑制作自己的选择器,但不建议对于日期选择器。


谢谢,我已经在设备上尝试过了,它以英国方式显示。 - user973067

2

当用文本书写月份时,英国人可能会先写月份(或至少这样做也是可以接受的)。

但是回答你的问题,我不知道如何更改日期选择器的显示格式,但你可以使用带有自定义委托和数据源的UIPickerView来创建自己的日期选择器。


当日期类似于20/01/11时,可以写成January 20 2011的形式(免责声明:我不是英国人,但我住在英国...) - wattson12
谢谢。是的,大多数情况下是日在前面,但月在前面也可以接受(只有当人们用美国方式写作时,如05/07/2012,才会令人困惑)。无论如何,感谢您的建议。 - user973067

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接