// 添加个文本框
UITextField *textfield = [[ UITextField alloc ] initWithFrame : CGRectMake ( 100 , 350 , 200 , 30 )];
[ self . view addSubview :textfield];
textfield. backgroundColor = [ UIColor grayColor ];
self . textfield = textfield;
// 弹出来一个 datepicker, 默认有宽高
UIDatePicker *datepick = [[ UIDatePicker alloc ] init ];
datepick. datePickerMode = UIDatePickerModeDate ; // 设置显示模式
datepick. locale = [ NSLocale localeWithLocaleIdentifier : @"zh-Hans" ]; // 设置语言
// 添加监听事件
[datepick addTarget : self action : @selector (change:) forControlEvents : UIControlEventValueChanged ]; // 当日期变化的时候
// 设置文本框的 inputview 属性
textfield. inputView = datepick;
datepick. minimumDate = [ NSDate dateWithTimeIntervalSince1970 : 0 ]; // 最大可选择时间
datepick. maximumDate = [ NSDate dateWithTimeIntervalSinceNow : 0 ]; // 最小可选择时间
//--------------------- 创建 picker 上面的工具条 -----------------------------------
// 注意 默认在弹框上方 , 所以前 3 个参数没有作用 , 只有高度有用
UIToolbar *toolbar = [[ UIToolbar alloc ] initWithFrame : CGRectMake ( 0 , 100 , 20 , 38 )];;
// 设置背景颜色
toolbar. backgroundColor = [ UIColor greenColor ];
// 创建一个 item, 确定按钮
UIBarButtonItem *item = [[ UIBarButtonItem alloc ] initWithTitle : @" 确定 " style : UIBarButtonItemStylePlain target : self action : @selector (endedit)]; // 缩回键盘
// 创建个弹簧 , 弹到右边去
UIBarButtonItem *fix = [[ UIBarButtonItem alloc ] initWithBarButtonSystemItem : UIBarButtonSystemItemFlexibleSpace target : nil action : nil ];
toolbar. tintColor = [ UIColor blackColor ]; // 设置工具条上的文字颜色
toolbar. items = @[ fix,item ] ;
textfield. inputAccessoryView = toolbar; // 文本框的属性
}
// 滚动日期选择器的时候
- ( void )change:( UIDatePicker *)datepick{
// 格式化日期
NSDateFormatter *date = [[ NSDateFormatter alloc ] init ];
date. dateFormat = @"yyyy/MM/dd" ; // 中间的 / 可以改变自己定义
// 转化成字符串
NSString *str1 = [date stringFromDate :datepick. date ];
// 给文本框赋值
self . textfield . text = str1;
}
// 缩回键盘
- ( void )endedit{
[ self . view endEditing : YES ];
}
- ( void )nextcontroller{
UIBarButtonItem *item = [[ UIBarButtonItem alloc ] initWithBarButtonSystemItem : UIBarButtonSystemItemAdd target : self action : @selector (click)];
self . navigationItem . rightBarButtonItem = item;
}
- ( void )click{
KBbirthController *birth = [[ KBbirthController alloc ] init ];
[ self . navigationController pushViewController :birth animated : YES ];
}
- ( void )viewWillAppear:( BOOL )animated{
[ super viewWillAppear :animated];
UILabel *label = [[ UILabel alloc ] initWithFrame : CGRectMake ( 100 , 100 , 200 , 200 )];
[ self . view addSubview :label];
label. numberOfLines = 0 ;
label. text = self . sqec ;
self . view . backgroundColor = [ UIColor whiteColor ];
}