关闭

UIPickerView实现年月选择器

标签: UIPickerView年月时间选择器UITextField弹出时间选择控件
2687人阅读 评论(0) 收藏 举报
分类:
刚做到一个UITextField弹出时间选择器选择年月的功能,UIDatePickerView并没有只选年月的,那就用UIPickerView自定义一个年月选择器,代码如下:

遵循UIPickerViewDelegate,UIPickerViewDataSource这两个代理先

UIView *limitDateView;
UIPickerView *limitPickerView;
NSString *thisYear;
NSString *thisMonth;

拿到当天的年月
<pre name="code" class="objc">NSDateFormatter *formmater = [[NSDateFormatter alloc] init];
    [formmater setDateFormat:@"yyyy-MM-dd"];
    
    DLog(@"%@",[formmater stringFromDate:[NSDate date]]);
    
    thisYear = [[formmater stringFromDate:[NSDate date]] componentsSeparatedByString:@"-"][0];
    thisMonth = [[formmater stringFromDate:[NSDate date]] componentsSeparatedByString:@"-"][1];



在选择的这个UITextFiled代理里面响应picker弹出
-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField{
    if (textField.tag == 100) {
        [self setUpLimitView];
        [textField resignFirstResponder];
        return NO;
    }else
    {
        return YES;
    }
}

pickerview的代理方法是关键自定义显示部分
- (void)setUpLimitView{
    [limitDateView removeFromSuperview];
    limitDateView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREENHEIGHT-192 - 64, SCREENWIDTH, 192)];
    limitDateView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    limitPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, SCREENWIDTH, 162)];
    
    //通过年月确定picker第一次显示年月顺便textfiled填入
    [limitPickerView selectRow:0 inComponent:[thisMonth integerValue]>=10?[thisMonth integerValue]:[[thisMonth substringFromIndex:1] integerValue] animated:YES];
    
    [limitPickerView selectRow:1 inComponent:[thisYear integerValue] animated:YES];

    expiryDateT.text = [NSString stringWithFormat:@"%@/%@",thisMonth,[thisYear substringFromIndex:2]];
    
    limitPickerView.delegate = self;
    limitPickerView.dataSource = self;
    [limitDateView addSubview:limitPickerView];
    
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 44)];
    toolbar.backgroundColor = [UIColor clearColor];
    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(clickHideLimit)];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(clickDoneLimit)];
    [toolbar setItems:@[cancelBtn, space, doneBtn]];
    [limitDateView addSubview:toolbar];
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = 0.3;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromTop;
    [limitDateView setAlpha:1.0f];
    [limitDateView.layer addAnimation:animation forKey:@"LimitAnimation"];
    [self.view addSubview:limitDateView];
}
#pragma mark - UIPickerViewDelegate
//告知在UIPickerView中有多少个滚轮
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
    
}
//告知在UIPickerView中,每个滚轮显示多少行的值
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return 12;
    }else
    {
        return 100;
    }
}
//告知每个滚轮的每个行显示的内容是什么
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component==0) {
        return [NSString stringWithFormat:@"%ld月",row+1];
    }else
    {
        return [NSString stringWithFormat:@"%ld年",[thisYear intValue]+row];
    }
}

//当滚轮停止滚动时,会回调这个函数,告知你当前选择了哪个滚轮的哪个值
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSLog(@"%ld月,%ld年",(long)[pickerView selectedRowInComponent:0]+1,(long)[pickerView selectedRowInComponent:1]+[thisYear integerValue]);
    NSString *monthStr = [pickerView selectedRowInComponent:0]+1<10?[NSString stringWithFormat:@"0%ld",(long)[pickerView selectedRowInComponent:0]+1]:[NSString stringWithFormat:@"%ld",(long)[pickerView selectedRowInComponent:0]+1];
    
    NSString *yearStr = [[NSString stringWithFormat:@"%ld",(long)[pickerView selectedRowInComponent:1]+[thisYear integerValue]] substringFromIndex:2];
    
    expiryDateT.text = [NSString stringWithFormat:@"%@/%@",monthStr,yearStr];
}

#pragma mark - toolbar事件
- (void)clickHideLimit {
    [limitDateView removeFromSuperview];
    expiryDateT.text = @"";
}
- (void)clickDoneLimit {
    [limitDateView removeFromSuperview];
}

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

UIPickerView实现年月选择器

刚做到一个UITextField弹出时间选择器选择年月的功能,UIDatePickerView并没有只选年月的,那就用UIPickerView自定义一个年月选择器,代码如下: 遵循UIPickerV...
  • echo_chris
  • echo_chris
  • 2016-08-29 11:45
  • 2687

iOS 使用UIPickerView三级联动实现选择日期年月日

这里面有很多自定义的宏,效果图在下面,如果想要这个效果的朋友,不妨花几分钟把里面的宏改下等不报错了,直接用这下面的代码调用就可以了! 调用代码 SelectTimeV *selectTimeV = ...
  • johnay_xiao
  • johnay_xiao
  • 2017-07-24 17:16
  • 1661

swift3.0 包含年月日十分 时间选择器的实现

相信很多app需要显示年月日时分,但是系统自带的datapicker选择时分后没有年月,故需要这样的 项目上不需要显示秒,所以隐藏了秒,需要显示秒的同学,可以参照代码隐藏即可 效果图: vie...
  • liyanjun201
  • liyanjun201
  • 2017-02-08 12:09
  • 1383

iOS省市区选择器UIPickerView

  • 2016-01-27 09:42
  • 125KB
  • 下载

Android 日期选择器,实现年月日三级联动

最近项目里面用到了一个日期选择器,实现现在主流的WheelView滑动选择,整理了下,做了个Demo.废话不多说,直接上代码. 主布局:activity_main.xml ...
  • lulusayhi
  • lulusayhi
  • 2017-03-08 11:19
  • 713

Android 日期选择器,实现年月日三级联动

最近项目里面用到了一个日期选择器,实现现在主流的WheelView滑动选择,整理了下,做了个Demo.废话不多说,直接上代码: 主布局:activity_main.xml <RelativeLayou...
  • hubeiqiyuan
  • hubeiqiyuan
  • 2016-08-08 16:21
  • 5023

【iOS】UIPickerView -- 地址选择器:省/市/区

由于项目的需要,需要对之前的地址选择器做改进。之前的地址选择器只能显示地址的省市区的名字等信息,不能显示其他ID类的信息。这次改进是使用新的数据源,关于数据源从后台获取和写入数据库我就不仔细说明了,代...
  • zhuming3834
  • zhuming3834
  • 2016-02-17 20:18
  • 10129

疯狂ios讲义之选择器(UIPickerView)

转载自http://www.oschina.net/question/262659_142046?sort=time UIPickerView也是一个选择器控件,它比UIDatePicker...
  • guofatfat
  • guofatfat
  • 2015-08-31 15:25
  • 415

swift3-省、市、区地址选择器,plist格式数据(UIPickerView)

This article mainly introduces the basic usage of UIPickerView, we can obtain address information by...
  • nc_jingjing
  • nc_jingjing
  • 2017-03-21 16:04
  • 319

iOS之UIPickerView的使用--单列选择器

1、.h #import @interface FKViewController : UIViewController @property (strong, nonatomic) IBOutlet...
  • yuxikuo_1
  • yuxikuo_1
  • 2015-01-05 11:05
  • 1046
    个人资料
    • 访问:25284次
    • 积分:375
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:2篇
    • 译文:1篇
    • 评论:4条
    文章分类
    最新评论