【问题标题】:iPhone:DatePicker dd/mm/yyyyiPhone:日期选择器 dd/mm/yyyy
【发布时间】:2012-02-14 22:21:57
【问题描述】:

当用户单击文本字段时,我正在使用带有弹出窗口的 DatePicker。但是,选择器首先显示月份,而不是日期,而且这不是英国人写日期的方式。 有没有办法将 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];

}

【问题讨论】:

    标签: iphone objective-c ios datepicker


    【解决方案1】:

    UIDatePicker 默认使用[NSLocale currentLocale] 指示的任何内容。

    UIDatePicker 上的 locale 属性在 iOS 5 中已弃用,但我相信您可以执行以下操作:

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

    应该工作。如果没有,请file a bug,我会修复它。 :)

    【讨论】:

    • 谢谢戴夫,但这由于某种原因不起作用。还有其他人有这个困境吗?我在这里看到了 BBog 的回答,看起来很令人沮丧:stackoverflow.com/questions/9638698/…
    【解决方案2】:

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

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

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

    如果您想要特定格式,请考虑制作自己的选择器,但非常不鼓励使用日期选择器。

    【讨论】:

    • 感谢我在设备上试用过,它以英国方式显示。
    【解决方案3】:

    当用文字书写月份时,英国人可能会将月份放在首位(或者至少可以接受)。

    但要回答您的问题,我不知道有什么方法可以更改日期选择器的显示格式,但您可以使用带有自定义委托和数据源的 UIPickerView 让您自己拥有。

    【讨论】:

    • 当日期是 2011 年 1 月 1 日,但写 2011 年 1 月 20 日可能还可以(免责声明:我不是英国人,但我住在英国......)
    • 谢谢。是的,大部分时间是第一天,但​​可以接受第一个月(只有当人们以美国方式写作时,例如 2012 年 5 月 7 日,这才会令人困惑)无论如何感谢您的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 2014-07-06
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多