UIDatePicker

一、简介

1.继承关系
UIDatePicker继承自UIControl,它和UIPickerView界面有点一样,但是没有直接的联系。
2.使用场景
一般配合UITextField使用,用于选择日期。
3.基本概念
地区:设置了地区,就使用那个地区的语言显示日期
日历:用于处理时间相关问题
时区:地理名称,区域时间
4. 界面
这里写图片描述

二、常见属性

// 使用地区
@property (nonatomic, retain) NSLocale *locale; 
// 日历
@property (nonatomic, copy) NSCalendar *calendar;
// 时区
@property (nonatomic, retain) NSTimeZone *timeZone;
// 时间
@property (nonatomic, retain) NSDate *date;
// 最小时间
@property (nonatomic, retain) NSDate *minimumDate;
// 最大时间
@property (nonatomic, retain) NSDate *maximumDate;

三、常见方法

//设置UIDatePicker所显示的选中时间
- (void)setDate:(NSDate *)date animated:(BOOL)animated;

四、使用方法

UITextField和UIDatePicker的配合使用

//自定义生日键盘
- (void)setUpBirthdayKeyboard {

  self.dataPicker = [[UIDatePicker alloc] init];
  //设置dataPicker显示类型
  self.dataPicker.datePickerMode = UIDatePickerModeDate;
  //设置文本框在编辑时弹出dataPicker
  self.birthdayTextField.inputView = self.dataPicker;
  //监听dataPicker的滚动
  [self.dataPicker addTarget:self action:@selector(Changed:) forControlEvents:UIControlEventValueChanged];

}

//dataPicker的滚动事件
- (void)Changed:(UIDatePicker *)dataPicker {
  //日期格式对象
  NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
  //设置日期格式对象的日期格式
  dateFormatter.dateFormat = @"yyyy-MM-dd";
  //日期转字符串并赋值给birthdayTextField
  self.birthdayTextField.text = [dateFormatter stringFromDate:dataPicker.date];

}

#pragma mark - <UITextFieldDelegate>

//是否允许更改字符串;设置UITextField有光标闪动,但不能输入
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  return NO;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值