iPhone:日期选择器 dd/mm/yyyy
当用户单击文本字段时,我使用带有弹出窗口的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,
UIDatePicker
使用[NSLocale currentLocale]
指定的任何内容。UIDatePicker
上的locale
属性在 iOS 5 中已被弃用,但我相信您可以执行以下操作:这应该可以工作。如果没有,请提交错误,我会修复它。 :)
The
UIDatePicker
, by default, uses whatever[NSLocale currentLocale]
dictates.The
locale
property onUIDatePicker
was deprecated in iOS 5, but I believe that you could do the following:This should work. If it doesn't, please file a bug and I'll fix it. :)
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 :
If you want a specific format, consider making your own picker but it is very discouraged for a date picker.
当用文本书写月份时,英国人可能会将月份放在第一位(或者至少可以接受)。
但要回答您的问题,我不知道如何更改日期选择器的显示格式,但您可以使用带有自定义委托和数据源的 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.