iOS-选择生日,初始化日期选择器

#pragma mark — 选择生日
- (void)editBrithday {
    UIAlertView * _brithdayAlert = [UIAlertView new];
    _brithdayAlert.title = @"编辑生日";
    [_brithdayAlert addButtonWithTitle: @"取消"];
    [_brithdayAlert addButtonWithTitle: @"确认"];
    
    //    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
    [self initDatePicker];
    _datePicker.datePickerMode = UIDatePickerModeDate;//选择年月日模式
    [_brithdayAlert setValue:_datePicker forKey:@"accessoryView"];
    [_brithdayAlert show];
}

// 初始化日期选择器
- (void)initDatePicker
{
    _datePicker = [[UIDatePicker alloc]init];
    _datePicker.frame = CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height*175/340);
    [_datePicker addTarget:self action:@selector(timeChanged:) forControlEvents:UIControlEventValueChanged];
    
    NSArray *strArray = [_arrTitle[2] componentsSeparatedByString:@":"];
    NSString * brithdayStr = strArray[1];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    _currentBrithdayDate = [dateFormatter dateFromString:brithdayStr];
    _datePicker.date = _currentBrithdayDate;
    _datePicker.maximumDate = [NSDate date];
    
}

// 停止滚动的时候的方法
- (void)timeChanged:(UIDatePicker *)datePicker
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *text = [formatter stringFromDate:datePicker.date];
    _birthdayStr = text;
    
    //刷生日进表
    [_arrTitle replaceObjectAtIndex:2 withObject:[NSString stringWithFormat:@"生日:%@", _birthdayStr]];
    
    //根据获得的年与日拿到我们需要的月日数据进行星座转换
    NSArray *strArray = [_birthdayStr componentsSeparatedByString:@"-"];
    NSString * month = strArray[1];
    NSString * day = strArray[2];
    NSString * constellation = [self calculateConstellationWithMonth: [month integerValue] day:[day integerValue]];
    [_arrTitle replaceObjectAtIndex:3 withObject:[NSString stringWithFormat:@"星座:%@", constellation]];
    
    
    [_tableView reloadData];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值