UIDatePicker

UIDatePicker相关

[objc]  view plain  copy
  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     [self createOneDatePicker];  
  4. }  
[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #pragma mark - 创建时间选择器  
  2. -(void)createOneDatePicker{  
  3.     CGRect screen = [[UIScreen mainScreen] bounds];  
  4.       
  5.     /// 1.日期选择器  
  6.     CGFloat datePickerWidth = 320;  
  7.     CGFloat datePickerHeight = 167;  
  8.     self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(00, datePickerWidth, datePickerHeight)];  
  9.     //zh-Hans简体中文  
  10.     self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh-Hans"];  
  11.     //设置日期时间模式  
  12.     self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;  
  13.       
  14.     /** 
  15.         UIDatePickerModeTime,时间模式 
  16.         UIDatePickerModeDate,日期模式 
  17.         UIDatePickerModeDateAndTime,时间日期模式 
  18.         UIDatePickerModeCountDownTimer,倒计时模式 
  19.      */  
  20.     [self.view addSubview:self.datePicker];  
  21.       
  22.     /// 2.添加标签  
  23.     CGFloat labelWidth = 200;  
  24.     CGFloat labelHeight = 21;  
  25.     CGFloat labelTopView = 281;  
  26.       
  27.     self.label = [[UILabel alloc] initWithFrame:CGRectMake((screen.size.width - labelWidth)/2 , labelTopView, labelWidth, labelHeight)];  
  28.       
  29.     self.label.text = @"Label";  
  30.     //字体左右居中  
  31.     self.label.textAlignment = NSTextAlignmentCenter;  
  32.     [self.view addSubview:self.label];  
  33.       
  34.     /// 3.Button按钮  
  35.     UIButton* button= [UIButton buttonWithType:UIButtonTypeSystem];  
  36.     [button setTitle:@"Button" forState:UIControlStateNormal];  
  37.       
  38.     CGFloat buttonWidth = 46;  
  39.     CGFloat buttonHeight = 30;  
  40.     CGFloat buttonTopView = 379;  
  41.       
  42.     button.frame = CGRectMake((screen.size.width - buttonWidth)/2 , buttonTopView, buttonWidth, buttonHeight);  
  43.     //指定事件处理方法  
  44.     [button addTarget:self action:@selector(onclick:) forControlEvents: UIControlEventTouchUpInside];  
  45.     [self.view addSubview:button];  
  46. }  
[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #pragma mark - 点击事件  
  2. - (void)onclick:(id)sender {  
  3.     NSDate * theDate = self.datePicker.date;  
  4.     NSLog(@"the date picked is: %@", [theDate descriptionWithLocale:[NSLocale currentLocale]]);  
  5.     NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init] ;  
  6.     dateFormatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";  
  7.     NSLog(@"the date formate is: %@", [dateFormatter stringFromDate:theDate]);  
  8.     self.label.text = [dateFormatter stringFromDate:theDate];  
  9.       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值