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

来源:互联网 发布:c语言小程序 编辑:程序博客网 时间:2024/06/06 10:57
 
   
// 添加个文本框
   
UITextField *textfield = [[UITextFieldalloc]initWithFrame:CGRectMake(100,350,200,30)];
    [
self.viewaddSubview:textfield];
    textfield.
backgroundColor= [UIColorgrayColor];
   
self.textfield= textfield;
   
   
// 弹出来一个datepicker, 默认有宽高
   
UIDatePicker *datepick = [[UIDatePickeralloc]init];
    datepick.
datePickerMode= UIDatePickerModeDate; // 设置显示模式
    datepick.
locale = [NSLocale localeWithLocaleIdentifier:@"zh-Hans"];//设置语言
   
// 添加监听事件
    [datepick
addTarget:selfaction:@selector(change:)forControlEvents:UIControlEventValueChanged];// 当日期变化的时候
   
//  设置文本框的inputview属性
    textfield.
inputView= datepick;
    datepick.
minimumDate= [NSDatedateWithTimeIntervalSince1970:0];//最大可选择时间
    datepick.
maximumDate= [NSDatedateWithTimeIntervalSinceNow:0];//最小可选择时间
   
   
//---------------------创建picker上面的工具条-----------------------------------
   
//  注意 默认在弹框上方,所以前3个参数没有作用,只有高度有用
   
UIToolbar *toolbar = [[UIToolbaralloc]initWithFrame:CGRectMake(0, 100, 20, 38)];;
   
// 设置背景颜色
    toolbar.
backgroundColor= [UIColorgreenColor];
   
//  创建一个item,确定按钮
   
UIBarButtonItem *item = [[UIBarButtonItemalloc]initWithTitle:@"确定"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(endedit)];// 缩回键盘
   
// 创建个弹簧, 弹到右边去
   
UIBarButtonItem *fix = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:nilaction:nil];
   
    toolbar.
tintColor= [UIColorblackColor];// 设置工具条上的文字颜色
    toolbar.
items = @[fix,item];
    textfield.
inputAccessoryView= toolbar; // 文本框的属性
}
// 滚动日期选择器的时候
- (
void)change:(UIDatePicker*)datepick{
   
//格式化日期
   
NSDateFormatter *date = [[NSDateFormatteralloc]init];
    date.
dateFormat = @"yyyy/MM/dd";// 中间的/可以改变自己定义
   
// 转化成字符串
   
NSString *str1 = [date stringFromDate:datepick.date];
   
   
// 给文本框赋值
   
self.textfield.text= str1;
   

}
// 缩回键盘
- (
void)endedit{

    [
self.viewendEditing:YES];

}

- (
void)nextcontroller{

   
UIBarButtonItem *item = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:@selector(click)];
   
   
self.navigationItem.rightBarButtonItem= item;


}
- (
void)click{
   
   
KBbirthController*birth = [[KBbirthControlleralloc]init];
   
    [
self.navigationControllerpushViewController:birthanimated:YES];
   



}

- (
void)viewWillAppear:(BOOL)animated{
    [
super viewWillAppear:animated];
   
   
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100,100,200,200)];
   
    [
self.viewaddSubview:label];
   
    label.
numberOfLines= 0;
   
    label.
text = self.sqec;
   
   
self.view.backgroundColor= [UIColorwhiteColor];

   

}
   
0 0
原创粉丝点击