IOS 日期选择器 UIDatePicker的使用

来源:互联网 发布:wind学生版mac 编辑:程序博客网 时间:2024/05/16 12:09

在这 我会给大家分享一个自己写的小程序,详细介绍它的使用...

 

如图:    最上面的 其实是一行cell       左边 起始时间 为标题label  ,右边的22:37 为副标题 label 

    功能,当点击这行cell的时候,会从弹出如图的这个一个日期选择器,然后 选择时间,再把这个时间设置给副标题显示出来

该如何实现呢??????


下面就不给大家引申其他方法了,,直接呈上我的这个思路 及 代码:


思路: 

就是在当前cell中添加一个文本框 (不设置他的frame 这样我们就看不到它了 )再让他变成第一响应者 (因为变成第一响应者之后 自然就会弹出键盘 ) 点击工具栏上的按钮时,再把键盘叫回去.然后把选择的时间设置给cell



/日期选择器

@property(nonatomic,strong)UIDatePicker *datePicker;


//工具栏

@property(nonatomic,strong)UIToolbar *toolBar;


//文本框

@property(nonatomic,strong)UITextField *textField;



   但是默认弹出的键盘样式 不是日期选择器这种类型的.所以,我们可以通过两句代码,修改 文本框弹出的键盘类型  :


-(UITextField *)textField

{

    if (_textField==nil) {

        _textField=[[UITextField alloc]init];

        //这行代码可以指定文本框弹出键盘的样式:

        _textField.inputView = self.datePicker;

        //这行代码可以为键盘指定一个工具栏

        _textField.inputAccessoryView = self.toolBar;


    }

    return _textField;

}

---------------------------------------

-(UIToolbar *)toolBar

{

    if (_toolBar==nil) {

        _toolBar=[[UIToolbar alloc]init];

        //工具栏的宽度是不用设置的,始终和屏幕一样宽,然后紧贴键盘上面,所以只用指定一下工具栏的高度即可.

        _toolBar.h=44; //这里的.h是导入了UIView的扩展类..

        _toolBar.barTintColor = [UIColor purpleColor];

         // 创建工具栏中的三个按钮

        //取消按钮

        UIBarButtonItem *itemCancel=[[UIBarButtonItem alloc]initWithTitle:@"取消"style:UIBarButtonItemStylePlain target:self action:@selector(didClickCancelKeyboardButton) ];

        // 弹簧  UIBarButtonSystemItemFlexibleSpace 可伸缩的空间

        UIBarButtonItem *itemSpring = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

        // done

        UIBarButtonItem *itemDone = [[UIBarButtonItem allocinitWithTitle:@"完成"style:UIBarButtonItemStyleDone target:self action:@selector(didClickDoneButton)];

        _toolBar.items = @[itemCancel, itemSpring, itemDone];

    }

    return _toolBar;

}

-----------------------------------------------

-(UIDatePicker *)datePicker

{

    if (_datePicker==nil) {

        _datePicker=[[UIDatePicker alloc]init];

        //指定显示的区域模式   传一个国家的代号

        _datePicker.locale=[NSLocale localeWithLocaleIdentifier:@"zh-Hans"];

        //指定一个显示的模式:我们只用到时间


        _datePicker.datePickerMode=UIDatePickerModeTime;

//        UIDatePickerModeTime,     时间      // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)

//        UIDatePickerModeDate,   日期        // Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007)

//        UIDatePickerModeDateAndTime,  日期和时间  // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)

//        UIDatePickerModeCountDownTimer,

    }

    return _datePicker;

}

---------------------------------------------------------

/ 选中某个cell后会执行    


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // 记录indexPath

    self.idxPath = indexPath;

    

    // 如果点击的是第0组, 那么什么都不做

    if (indexPath.section == 0return;

    

    // 如果点击的不是第0组, 那么再弹出键盘

    // 1. 创建文本框, 并且把文本框添加到当前的cell中, 同时让当前文本框变成第一响应者

    FDUITbaleViewCell *cell = (FDUITableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];    //这里用的是自定义cell 如上图中所示的cell的样式,那么具体使用那种cell的样式,我们下面对有图示

    [cell.contentView addSubview:self.textField];

    [self.textField becomeFirstResponder];

}

----------------------------------------------------------------------------------------------

//点击了工具栏上的取消按钮,然后把键盘叫回去

-(void)didClickCancelKeyboardButton

{

    //辞去第一响应者

    [self.view endEditing:YES];

     //移除单元格  避免了 每次点击单元格的时候  一个文本框 添加多次....     

     [self.textFiled removeFromSuperview];

}

--------------------------------

// 点击完成按钮

- (void)didClickDoneButton {

    // 1. 把键盘叫回去

    [self.view endEditing:YES];

    

    // 2. 把用户选择的时间设置给detailTextLabel

    // 2.1 获取用户选择的时间

    NSDate *date = self.datePicker.date;

    // 2.2 把用户选择的时间转换为字符串类型

    NSDateFormatter *formatter = [[NSDateFormatter allocinit];

    formatter.dateFormat = @"HH:mm";

    NSString *strTime = [formatter stringFromDate:date];

    

    // 2.3 把时间设置给当前选中的cell的detaisTextLabel

    // 2.3.1 获取当前选中的cell

   FDUITbaleViewCelll *cell = [self.tableView cellForRowAtIndexPath:self.idxPath];

    // cell.detailTextLabel.text = strTime;

    [cell setTimeWithText:strTime];    //setTimeWithText:strTime中实现了对这句代码的封装

    

    // 3. 把文本框从父容器中移除

    [self.textFiled removeFromSuperview];

}





1 0
原创粉丝点击