关闭

UIPickerView实现年月选择器

标签: UIPickerView年月时间选择器UITextField弹出时间选择控件
2347人阅读 评论(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网站的观点或立场
    个人资料
    • 访问:22713次
    • 积分:350
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:2篇
    • 译文:1篇
    • 评论:4条
    文章分类
    最新评论