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

发布于 2024-12-27 22:14:51 字数 2191 浏览 2 评论 0原文

当用户单击文本字段时,我使用带有弹出窗口的 DatePicker。但是,选择器首先显示月份,而不是日期,并且这不是英国的日期书写方式。 有没有办法将 DatePicker 中的日期格式设置为英式日期格式,例如 dd/mm/yyyy? 我正在使用 Actionsheet:

-(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];

}

I am using a DatePicker with popups when a user click a textField. However, the Picker displays Month first, not date first and it isn't British way to write a date.
Is there any way to set the date format in DatePicker to British way such as dd/mm/yyyy?
I am using an 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];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄荷港 2025-01-03 22:14:51

默认情况下,UIDatePicker 使用[NSLocale currentLocale] 指定的任何内容。

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

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

应该可以工作。如果没有,请提交错误,我会修复它。 :)

The UIDatePicker, by default, uses whatever [NSLocale currentLocale] dictates.

The locale property on UIDatePicker was deprecated in iOS 5, but I believe that you could do the following:

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

This should work. If it doesn't, please file a bug and I'll fix it. :)

落花随流水 2025-01-03 22:14:51

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

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

设置>一般>国际>区域格式

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

The UIDatePicker is made to adapt to the user's settings. Changing its format using the locale: method was possible but is now deprecated in iOS 5.0.

You can view your format in the settings of your iPhone :

Settings > General > International > Region Format

If you want a specific format, consider making your own picker but it is very discouraged for a date picker.

硬不硬你别怂 2025-01-03 22:14:51

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

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

When writing the month in text the british probably would put the month first (or at least it would be acceptable).

But to answer your question I'm not aware of a way to change the display format of the date picker, but you could make you're own using a UIPickerView with a custom delegate and data source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文