UIDatePickerView的完美使用 日期选取器

在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

效果图:



【原文:http://www.gowhich.com/blog/337】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值