Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

objective c - iPhone:DatePicker dd/mm/yyyy

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...