UIDatePicker

UIDatePicker相关

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createOneDatePicker];
}


#pragma mark - 创建时间选择器
-(void)createOneDatePicker{
    CGRect screen = [[UIScreen mainScreen] bounds];
    
    /// 1.日期选择器
    CGFloat datePickerWidth = 320;
    CGFloat datePickerHeight = 167;
    self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, datePickerWidth, datePickerHeight)];
    //zh-Hans简体中文
    self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh-Hans"];
    //设置日期时间模式
    self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    
    /**
        UIDatePickerModeTime,时间模式
        UIDatePickerModeDate,日期模式
        UIDatePickerModeDateAndTime,时间日期模式
        UIDatePickerModeCountDownTimer,倒计时模式
     */
    [self.view addSubview:self.datePicker];
    
    /// 2.添加标签
    CGFloat labelWidth = 200;
    CGFloat labelHeight = 21;
    CGFloat labelTopView = 281;
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake((screen.size.width - labelWidth)/2 , labelTopView, labelWidth, labelHeight)];
    
    self.label.text = @"Label";
    //字体左右居中
    self.label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.label];
    
    /// 3.Button按钮
    UIButton* button= [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"Button" forState:UIControlStateNormal];
    
    CGFloat buttonWidth = 46;
    CGFloat buttonHeight = 30;
    CGFloat buttonTopView = 379;
    
    button.frame = CGRectMake((screen.size.width - buttonWidth)/2 , buttonTopView, buttonWidth, buttonHeight);
    //指定事件处理方法
    [button addTarget:self action:@selector(onclick:) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview:button];
}


#pragma mark - 点击事件
- (void)onclick:(id)sender {
    NSDate * theDate = self.datePicker.date;
    NSLog(@"the date picked is: %@", [theDate descriptionWithLocale:[NSLocale currentLocale]]);
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init] ;
    dateFormatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    NSLog(@"the date formate is: %@", [dateFormatter stringFromDate:theDate]);
    self.label.text = [dateFormatter stringFromDate:theDate];
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值