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

本文介绍如何在iOS应用中实现日期选择功能。通过UITextField和UIDatePicker组件结合使用,可实现在文本框中选择并显示日期的效果。此外,还介绍了如何通过UIToolbar和UIBarButtonItem定制日期选择器的工具栏。
 
   
// 添加个文本框
   
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];

   

}
   
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值