UIToolBar以及日期选择器的基本用法

 
   
// 添加个文本框
   
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 ];

   

}
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值