在ios的开发中,日期选择器是个用的还是蛮多的一个部分,填写用户的出生日期,填写自己的计划,填写自己的约会安排,都是会有用到日期选择的地方的。

我认为这里面的关键点是设置日期的格式(datePickerMode),还有选择日期后去如何处理(这里我没有做),

浏览一下代码吧

UIDatePickerDemoViewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// UIDatePickerDemoViewController.m
// UIDatePickerDemo
//
// Created by david on 13-8-16.
// Copyright (c) 2013年 WalkerFree. All rights reserved.
//

#import "UIDatePickerDemoViewController.h"

@interface UIDatePickerDemoViewController ()

@end

@implementation UIDatePickerDemoViewController

- (void)viewDidLoad
{
[super viewDidLoad];


self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
self.navigationItem.rightBarButtonItem = BARBUTTON(@"操作", @selector(action:));

UISegmentedControl *seg= [[UISegmentedControl alloc] initWithItems:[@"Time Date DC Count" componentsSeparatedByString:@" "]];
seg.segmentedControlStyle = UISegmentedControlStyleBar;
seg.selectedSegmentIndex = 0;
self.navigationItem.titleView = seg;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - method of UIActionSheet
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
UIDatePicker *datePicker = (UIDatePicker *)[actionSheet viewWithTag:101];
NSDateFormatter *formattor = [[NSDateFormatter alloc] init];

switch ([(UISegmentedControl *) self.navigationItem.titleView selectedSegmentIndex]) {
case 0:
formattor.dateFormat = @"h:mm a";
break;
case 1:
formattor.dateFormat = @"dd MMMM yy";
break;
case 2:
formattor.dateFormat = @"MM/dd/YY h:mm a";
break;
case 3:
formattor.dateFormat = @"HH:mm";
break;
default:
break;
}
NSString *timestamp = [formattor stringFromDate:datePicker.date];
[(UILabel *)[self.view viewWithTag:103] setText:timestamp];

}

#pragma mark - method of self
-(void) action:(id)sender{
NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n";
UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:title
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:@"请选择日期", nil];
[actionsheet showInView:self.view];

UIDatePicker *datepicker = [[UIDatePicker alloc] init];
datepicker.tag = 101;
datepicker.datePickerMode = [(UISegmentedControl *)self.navigationItem.titleView selectedSegmentIndex];

[actionsheet addSubview:datepicker];
}
@end

UIDatePickerDemoViewController.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//
// UIDatePickerDemoViewController.h
// UIDatePickerDemo
//
// Created by david on 13-8-16.
// Copyright (c) 2013年 WalkerFree. All rights reserved.
//

#import <UIKit/UIKit.h>

#define COOKBOOK_PURPLE_COLOR [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
#define BARBUTTON(TITLE, SELECTOR) [[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR]

@interface UIDatePickerDemoViewController : UIViewController<UIActionSheetDelegate>

@end