FinishTimeView.m 22.2 KB
Newer Older
陈俊俊's avatar
陈俊俊 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//
//  FinishTimeView.m
//  XFFruit
//
//  Created by 陈俊俊 on 15/11/2.
//  Copyright © 2015年 Xummer. All rights reserved.
//

#import "FinishTimeView.h"
// 起始年数
#define kStartYear 1980
// 总年数
#define kYearCount 200
#define  DateViewHeight 246

@interface FinishTimeView ()
{
    UIView *_bgView;

}
// 定义数据属性
// 1. 年数组
@property (strong, nonatomic) NSArray *yearList;
// 2. 月数组
陈俊俊's avatar
陈俊俊 committed
25
@property (strong, nonatomic) NSMutableArray *monthList;
陈俊俊's avatar
陈俊俊 committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
// 3. 日数组
@property (strong, nonatomic) NSMutableArray *dayList;
// 时
@property (strong,nonatomic) NSArray *hourList;
// 分
@property (strong,nonatomic) NSArray *minuteList;
// 4. 选中的年数
@property (assign, nonatomic) NSInteger selectedYear;
// 5. 选中的月数
@property (assign, nonatomic) NSInteger selectedMonth;
@property (assign,nonatomic) NSInteger currentYear;
@property (assign,nonatomic) NSInteger currentMonth;
@property (assign,nonatomic) NSInteger currentDay;
@property (assign,nonatomic) NSInteger currentHour;
@property (assign,nonatomic) NSInteger currentMinute;

@property (nonatomic,strong) UIPickerView *pickerView;
@property (nonatomic,strong) NSString *dateStr;
陈俊俊's avatar
陈俊俊 committed
44 45
@property (nonatomic,strong) NSString *typeStr;

陈俊俊's avatar
陈俊俊 committed
46 47 48 49 50 51 52 53 54
@end

@implementation FinishTimeView

- (instancetype)initWithFrame:(CGRect)frame withDate:(NSString *)dateStr
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dateStr = dateStr;
陈俊俊's avatar
陈俊俊 committed
55
        self.typeStr = @"all";
陈俊俊's avatar
陈俊俊 committed
56 57 58 59 60
        [self buildLayout];
    }
    self.backgroundColor = [UIColor whiteColor];
    return self;
}
陈俊俊's avatar
陈俊俊 committed
61 62 63 64 65 66 67 68 69 70 71 72

- (instancetype)initWithFrame:(CGRect)frame withDate:(NSString *)dateStr type:(NSString *)type{
    self = [super initWithFrame:frame];
    if (self) {
        self.dateStr = dateStr;
        self.typeStr = type;
        [self buildLayout];
    }
    self.backgroundColor = [UIColor whiteColor];
    return self;
}

陈俊俊's avatar
陈俊俊 committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
- (void)buildLayout
{
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - DateViewHeight, ScreenSize.width, DateViewHeight)];
    _bgView.backgroundColor        = RGBA(239, 239, 239 ,1);
    [self addSubview:_bgView];
    
    UIButton *okBtn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(ScreenSize.width - 62, 0, 62, 28) target:self sel:@selector(okClick) tag:0 image:nil title:@"确定" titleColor:[UIColor blackColor] isCorner:NO corner:0 bgColor:RGBA(239, 239, 239 ,1)];
    [_bgView addSubview:okBtn];
    
    UIButton *cancelBtn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(0, 0, 62, 28) target:self sel:@selector(cancelClick) tag:0 image:nil title:@"取消" titleColor:[UIColor blackColor] isCorner:NO corner:0 bgColor:RGBA(239, 239, 239 ,1)];
    [_bgView addSubview:cancelBtn];

    UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 30,ScreenSize.width, DateViewHeight - 30)];
    pickerView.backgroundColor = [UIColor whiteColor];
    // 指定数据源
    [pickerView setDataSource:self];
    // 指定代理
    [pickerView setDelegate:self];
    // PickerView默认是没有选中标示的
    [pickerView setShowsSelectionIndicator:YES];
    self.pickerView = pickerView;
    [_bgView addSubview:pickerView];
    
    CLog(@"====%@",self.dateStr);

陈俊俊's avatar
陈俊俊 committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    if ([self.typeStr isEqualToString:@"all"]) {
        self.currentYear = [[self.dateStr substringWithRange:NSMakeRange(0,4)] intValue];
        self.currentMonth = [[self.dateStr substringWithRange:NSMakeRange(5,2)] intValue];
        self.currentDay = [[self.dateStr substringWithRange:NSMakeRange(8,2)] intValue];
        self.currentHour = [[self.dateStr substringWithRange:NSMakeRange(11,2)] intValue];
        self.currentMinute = [[self.dateStr substringWithRange:NSMakeRange(14,2)] intValue];
        // 3. 初始化数据
        // 1) 年数据
        NSMutableArray *yearArray = [NSMutableArray arrayWithCapacity:kYearCount];
        for (int i = 0; i < kYearCount; i++) {
            NSString *str = [NSString stringWithFormat:@"%d年", i + kStartYear];
            
            [yearArray addObject:str];
        }
        self.yearList = yearArray;
陈俊俊's avatar
陈俊俊 committed
113
        
陈俊俊's avatar
陈俊俊 committed
114 115 116 117 118 119 120 121
        // 2) 月数据
        NSMutableArray *monthArray = [NSMutableArray arrayWithCapacity:12];
        for (int i = 1; i <= 12; i++) {
            NSString *str = [NSString stringWithFormat:@"%d月", i];
            
            [monthArray addObject:str];
        }
        self.monthList = monthArray;
陈俊俊's avatar
陈俊俊 committed
122
        
陈俊俊's avatar
陈俊俊 committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        // 时
        NSMutableArray *hourArray = [NSMutableArray arrayWithCapacity:24];
        for (int i = 0; i < 24; i++) {
            NSString *str = [NSString stringWithFormat:@"%d时",i];
            [hourArray addObject:str];
        }
        self.hourList = hourArray;
        
        // 分
        NSMutableArray *minuteArray = [NSMutableArray arrayWithCapacity:60];
        for (int i = 0; i < 60; i++) {
            NSString *str = [NSString stringWithFormat:@"%d分",i];
            [minuteArray addObject:str];
        }
        self.minuteList = minuteArray;
        
        // 初始化选中年、月
        self.currentYear = [[self.dateStr substringWithRange:NSMakeRange(0,4)] intValue];
        self.currentMonth = [[self.dateStr substringWithRange:NSMakeRange(5,2)] intValue];
        self.currentDay = [[self.dateStr substringWithRange:NSMakeRange(8,2)] intValue];
        self.currentHour = [[self.dateStr substringWithRange:NSMakeRange(11,2)] intValue];
        
        [pickerView selectRow:(self.currentYear - kStartYear) inComponent:0 animated:YES];
        [pickerView selectRow:(self.currentMonth - 1) inComponent:1 animated:YES];
        [pickerView selectRow:(self.currentDay - 1) inComponent:2 animated:YES];
        [pickerView selectRow:self.currentHour inComponent:3 animated:YES];
        [pickerView selectRow:(self.currentMinute) inComponent:4 animated:YES];
        
        [self pickerView:pickerView didSelectRow:self.currentYear - kStartYear inComponent:0];
        [self pickerView:pickerView didSelectRow:(self.currentMonth - 1) inComponent:1];
        [self pickerView:pickerView didSelectRow:(self.currentDay - 1) inComponent:2];
        [self pickerView:pickerView didSelectRow:(self.currentHour - 1) inComponent:3];
        [self pickerView:pickerView didSelectRow:(self.currentMinute -1) inComponent:4];

    }else if([self.typeStr isEqualToString:@"month"]){
        self.currentYear = [[self.dateStr substringWithRange:NSMakeRange(0,4)] intValue];
        self.currentMonth = [[self.dateStr substringWithRange:NSMakeRange(5,2)] intValue];
        
        NSMutableArray *yearArray = [NSMutableArray arrayWithCapacity:kYearCount];
        for (int i = 0; i < kYearCount; i++) {
            NSString *str = [NSString stringWithFormat:@"%d年", i + kStartYear];
            
            [yearArray addObject:str];
        }
        self.yearList = yearArray;
        
        // 2) 月数据
        NSMutableArray *monthArray = [NSMutableArray arrayWithCapacity:12];
        for (int i = 1; i <= 12; i++) {
            NSString *str = [NSString stringWithFormat:@"%d月", i];
            
            [monthArray addObject:str];
        }
        self.monthList = monthArray;
        // 初始化选中年、月
        self.currentYear = [[self.dateStr substringWithRange:NSMakeRange(0,4)] intValue];
        self.currentMonth = [[self.dateStr substringWithRange:NSMakeRange(5,2)] intValue];
        
        
        [pickerView selectRow:(self.currentYear - kStartYear) inComponent:0 animated:YES];
        [pickerView selectRow:(self.currentMonth - 1) inComponent:1 animated:YES];
        
        [self pickerView:pickerView didSelectRow:self.currentYear - kStartYear inComponent:0];
        [self pickerView:pickerView didSelectRow:(self.currentMonth - 1) inComponent:1];
        
    }else if([self.typeStr isEqualToString:@"day"]){
        self.currentYear = [[self.dateStr substringWithRange:NSMakeRange(0,4)] intValue];
        self.currentMonth = [[self.dateStr substringWithRange:NSMakeRange(5,2)] intValue];
        self.currentDay = [[self.dateStr substringWithRange:NSMakeRange(8,2)] intValue];
        
        NSMutableArray *yearArray = [NSMutableArray arrayWithCapacity:kYearCount];
        for (int i = 0; i < kYearCount; i++) {
            NSString *str = [NSString stringWithFormat:@"%d年", i + kStartYear];
            
            [yearArray addObject:str];
        }
        self.yearList = yearArray;
        
        // 2) 月数据
        NSMutableArray *monthArray = [NSMutableArray arrayWithCapacity:12];
        for (int i = 1; i <= 12; i++) {
            NSString *str = [NSString stringWithFormat:@"%d月", i];
            
            [monthArray addObject:str];
        }
        self.monthList = monthArray;
        // 初始化选中年、月
        // 初始化选中年、月
        self.currentYear = [[self.dateStr substringWithRange:NSMakeRange(0,4)] intValue];
        self.currentMonth = [[self.dateStr substringWithRange:NSMakeRange(5,2)] intValue];
        self.currentDay = [[self.dateStr substringWithRange:NSMakeRange(8,2)] intValue];

        
        [pickerView selectRow:(self.currentYear - kStartYear) inComponent:0 animated:YES];
        [pickerView selectRow:(self.currentMonth - 1) inComponent:1 animated:YES];
        [pickerView selectRow:(self.currentDay - 1) inComponent:2 animated:YES];

        [self pickerView:pickerView didSelectRow:self.currentYear - kStartYear inComponent:0];
        [self pickerView:pickerView didSelectRow:(self.currentMonth - 1) inComponent:1];
        [self pickerView:pickerView didSelectRow:(self.currentDay - 1) inComponent:2];
    }else if ([self.typeStr isEqualToString:@"week"]){
        self.currentYear = [[self.dateStr substringWithRange:NSMakeRange(0,4)] intValue];

        
        NSMutableArray *yearArray = [NSMutableArray arrayWithCapacity:kYearCount];
        for (int i = 0; i < kYearCount; i++) {
            NSString *str = [NSString stringWithFormat:@"%d年", i + kStartYear];
            
            [yearArray addObject:str];
        }
        self.yearList = yearArray;
        
        self.currentYear = [[self.dateStr substringWithRange:NSMakeRange(0,4)] intValue];
        self.selectedYear = self.currentYear;
237
        self.currentMonth = [[self.dateStr substringWithRange:NSMakeRange(5,2)] intValue];
陈俊俊's avatar
陈俊俊 committed
238 239 240 241 242 243 244 245
        
        
        [pickerView selectRow:(self.currentYear - kStartYear) inComponent:0 animated:YES];
        [pickerView selectRow:(self.currentMonth - 1) inComponent:1 animated:YES];
        
        [self pickerView:pickerView didSelectRow:self.currentYear - kStartYear inComponent:0];
        [self pickerView:pickerView didSelectRow:(self.currentMonth - 1) inComponent:1];

陈俊俊's avatar
陈俊俊 committed
246
    }
陈俊俊's avatar
陈俊俊 committed
247

陈俊俊's avatar
陈俊俊 committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    
}

#pragma mark - 私有方法
#pragma mark 判断闰年
- (BOOL)isLeapYear:(NSInteger)year
{
    // 如果年数能被4整除,同时不能被100整除
    // 能被400整除的是闰年
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

#pragma mark 创建日数组
// 此方法的返回值是void,是为了不改变dayList的指针
- (void)createDayListWithYear:(NSInteger)year month:(NSInteger)month
{
    // 日数组是懒加载,需要时在创建
    if (self.dayList == nil) {
        // 在开发时尽可能要去控制NSMutableArray的容量
        self.dayList = [NSMutableArray arrayWithCapacity:31];
    } else {
        [self.dayList removeAllObjects];
    }
    
    // 初始化日数组
    /**
     年:是否是闰年
     月:大月,小月
     */
    NSInteger days = 31;
    if ([self isLeapYear:year] && month == 2) {
        // 闰年的2月
        days = 29;
    } else if (2 == month) {
        // 非闰年的2月
        days = 28;
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        // 小月
        days = 30;
    }
    
陈俊俊's avatar
陈俊俊 committed
289
    for (int i = 1; i <= days; i++) {
陈俊俊's avatar
陈俊俊 committed
290 291 292 293 294
        NSString *str = [NSString stringWithFormat:@"%d日", i];
        
        [self.dayList addObject:str];
    }
}
陈俊俊's avatar
陈俊俊 committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
- (void)getWeekByYear:(NSInteger)year{
    // 日数组是懒加载,需要时在创建
    if (self.monthList == nil) {
        // 在开发时尽可能要去控制NSMutableArray的容量
        self.monthList = [NSMutableArray arrayWithCapacity:53];
    } else {
        [self.monthList removeAllObjects];
    }
    
    NSInteger weeks = [IBTCommon getWeeks:year];
    for (int i = 1; i <= weeks; i++) {
        NSString *str = [NSString stringWithFormat:@"第%d周", i];
        [self.monthList addObject:str];
    }
}


陈俊俊's avatar
陈俊俊 committed
312 313 314 315
#pragma mark - PickerView的数据源方法
#pragma mark 指定列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
陈俊俊's avatar
陈俊俊 committed
316 317 318 319 320 321 322 323
    if([self.typeStr isEqualToString:@"all"]){
        return 5;

    }else if([self.typeStr isEqualToString:SaleEnMonth]|| [self.typeStr isEqualToString:SaleEnWeek]){
        return 2;
    }else{
        return 3;
    }
陈俊俊's avatar
陈俊俊 committed
324 325 326 327 328
}

#pragma mark 指定行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
陈俊俊's avatar
陈俊俊 committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    if ([self.typeStr isEqualToString:@"all"]) {
        if (0 == component) {
            // 年
            return self.yearList.count;
        } else if (1 == component) {
            // 月
            return self.monthList.count;
        } else if (2 == component) {
            // 日
            // 需要创建日数组-懒加载
            [self createDayListWithYear:self.selectedYear month:self.selectedMonth];
            
            return self.dayList.count;
        }
        else if (3 == component) {
            // 时
            return self.hourList.count;
        } else // 分
        {
            return self.minuteList.count;
        }

    }else if([self.typeStr isEqualToString:SaleEnMonth]){
        if (component == 0) {
            // 年
            return self.yearList.count;
        } else {
            // 月
            return self.monthList.count;
        }
    }else if([self.typeStr isEqualToString:SaleEnDay]){
        if (0 == component) {
            // 年
            return self.yearList.count;
        } else if (1 == component) {
            // 月
            return self.monthList.count;
        } else{
            // 日
            // 需要创建日数组-懒加载
            [self createDayListWithYear:self.selectedYear month:self.selectedMonth];
            
            return self.dayList.count;
        }
    }else{
        if (0 == component) {
            // 年
            return self.yearList.count;
        } else{
            // 月
            [self getWeekByYear:self.selectedYear];
            return self.monthList.count;
        }

陈俊俊's avatar
陈俊俊 committed
383 384 385 386 387 388 389
    }
}

#pragma mark - UIPickerViewDelegate
#pragma mark 指定component列row行的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
陈俊俊's avatar
陈俊俊 committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    if ([self.typeStr isEqualToString:@"all"]) {
        if (0 == component) {
            return self.yearList[row];
        } else if (1 == component) {
            return self.monthList[row];
        } else if (2 == component){
            return self.dayList[row];
        }
        else if (3 == component) {
            return self.hourList[row];
        } else {
            return self.minuteList[row];
        }
    }else if([self.typeStr isEqualToString:SaleEnMonth]|| [self.typeStr isEqualToString:SaleEnWeek] ){
        if (0 == component) {
            return self.yearList[row];
        }else{
            return self.monthList[row];
        }
    }else{
        if (0 == component) {
            return self.yearList[row];
        } else if (1 == component) {
            return self.monthList[row];
        } else{
            return self.dayList[row];
        }
陈俊俊's avatar
陈俊俊 committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430
    }
}


- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component == 0) {
        return 80;
    }
    return 50;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
陈俊俊's avatar
陈俊俊 committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
    if ([self.typeStr isEqualToString:@"all"] || [self.typeStr isEqualToString:@"day"]) {
        // 当用户在月份列选择数值时,需要刷新日期的数组
        self.selectedYear = [pickerView selectedRowInComponent:0] + kStartYear;
        self.selectedMonth = [pickerView selectedRowInComponent:1] + 1;
        NSInteger resetRow = [pickerView selectedRowInComponent:2];
        if (component == 1) {
            [pickerView reloadComponent:2];
            [self pickerView:pickerView didSelectRow:resetRow inComponent:2];
        } else if (component == 0 && self.selectedMonth == 2) {
            // 如果用户是在年列选择的数值,同时月份中的当前数值是2月,需要刷新数据
            [pickerView reloadComponent:2];
            [self pickerView:pickerView didSelectRow:resetRow inComponent:2];
        }
    }else if ([self.typeStr isEqualToString:SaleEnWeek]) {
        // 当用户在月份列选择数值时,需要刷新日期的数组
        self.selectedYear = [pickerView selectedRowInComponent:0] + kStartYear;
        NSInteger resetRow = [pickerView selectedRowInComponent:1];
        if (component == 0) {
            [pickerView reloadComponent:1];
            [self pickerView:pickerView didSelectRow:resetRow inComponent:1];
        }
陈俊俊's avatar
陈俊俊 committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *myView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 330, 30)];
    myView.textAlignment = NSTextAlignmentCenter;
    myView.font = [UIFont systemFontOfSize:16];
    switch (component) {
        case 0:
            myView.text = self.yearList[row];
            break;
        case 1:
            myView.text = self.monthList[row];
            break;
        case 2:
            myView.text = self.dayList[row];
            break;
        case 3:
            myView.text = self.hourList[row];
            break;
        case 4:
            myView.text = self.minuteList[row];
            break;
        default:
            break;
    }
    return myView;
}

- (NSString *)getDate
{
    NSInteger yearRow = [_pickerView selectedRowInComponent:0];
    NSString *year = [self.yearList[yearRow] stringByReplacingOccurrencesOfString:@"年" withString:@""];
    NSInteger monthRow = [_pickerView selectedRowInComponent:1];
    NSString *month = [self.monthList[monthRow] stringByReplacingOccurrencesOfString:@"月" withString:@""];
    NSInteger dayRow = [_pickerView selectedRowInComponent:2];
    NSString *day = [self.dayList[dayRow] stringByReplacingOccurrencesOfString:@"日" withString:@""];
    NSInteger hourRow = [_pickerView selectedRowInComponent:3];
    NSString *hour = [self.hourList[hourRow] stringByReplacingOccurrencesOfString:@"时" withString:@""];
    NSInteger minuteRow = [_pickerView selectedRowInComponent:4];
    NSString *minute = [self.minuteList[minuteRow] stringByReplacingOccurrencesOfString:@"分" withString:@""];
    NSArray *array1 = [NSArray arrayWithObjects:year,month,day, nil];
    NSArray *array2 = [NSArray arrayWithObjects:hour,minute, nil];
    NSString *date1 = [array1 componentsJoinedByString:@"-"];
    NSString *date2 = [array2 componentsJoinedByString:@":"];
陈俊俊's avatar
陈俊俊 committed
498
    NSString *date = [NSString stringWithFormat:@"%@ %@:00",date1,date2];
陈俊俊's avatar
陈俊俊 committed
499
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
陈俊俊's avatar
陈俊俊 committed
500
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
陈俊俊's avatar
陈俊俊 committed
501 502 503 504 505 506
    
    return [formatter stringFromDate:[formatter dateFromString:date]];
    
}

- (void)okClick{
陈俊俊's avatar
陈俊俊 committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
    if ([self.typeStr isEqualToString:@"all"]) {
        NSString *dateStr = [self getDate];
        [self.delegate okTimeView:dateStr withType:@"all"];
    }else if([self.typeStr isEqualToString:SaleEnMonth]){
        NSString *dateStr = [self getDateByMonth];
        [self.delegate okTimeView:dateStr withType:SaleEnMonth];
    }else if([self.typeStr isEqualToString:SaleEnDay]){
        NSString *dateStr = [self getDateByDay];
        [self.delegate okTimeView:dateStr withType:SaleEnDay];
    }else if([self.typeStr isEqualToString:SaleEnWeek]){
        NSString *dateStr = [self getDateByWeek];
        [self.delegate okTimeView:dateStr withType:SaleEnWeek];
    }
}

- (NSString *)getDateByMonth{
    NSInteger yearRow = [_pickerView selectedRowInComponent:0];
    NSString *year = [self.yearList[yearRow] stringByReplacingOccurrencesOfString:@"年" withString:@""];
    NSInteger monthRow = [_pickerView selectedRowInComponent:1];
    NSString *month = [self.monthList[monthRow] stringByReplacingOccurrencesOfString:@"月" withString:@""];
    
    NSArray *array1 = [NSArray arrayWithObjects:year,month, nil];
    NSString *date1 = [array1 componentsJoinedByString:@"-"];
    NSString *date = [NSString stringWithFormat:@"%@",date1];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM";
    
    return [formatter stringFromDate:[formatter dateFromString:date]];
}

- (NSString *)getDateByDay{
    NSInteger yearRow = [_pickerView selectedRowInComponent:0];
    NSString *year = [self.yearList[yearRow] stringByReplacingOccurrencesOfString:@"年" withString:@""];
    NSInteger monthRow = [_pickerView selectedRowInComponent:1];
    NSString *month = [self.monthList[monthRow] stringByReplacingOccurrencesOfString:@"月" withString:@""];
    NSInteger dayRow = [_pickerView selectedRowInComponent:2];
    NSString *day = [self.dayList[dayRow] stringByReplacingOccurrencesOfString:@"日" withString:@""];
    
    NSArray *array1 = [NSArray arrayWithObjects:year,month,day, nil];
    NSString *date1 = [array1 componentsJoinedByString:@"-"];
    NSString *date = [NSString stringWithFormat:@"%@",date1];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    
    return [formatter stringFromDate:[formatter dateFromString:date]];
}

- (NSString *)getDateByWeek{
    NSInteger yearRow = [_pickerView selectedRowInComponent:0];
    NSString *year = [self.yearList[yearRow] stringByReplacingOccurrencesOfString:@"年" withString:@""];
    NSInteger monthRow = [_pickerView selectedRowInComponent:1];
560
    
陈俊俊's avatar
陈俊俊 committed
561 562
    NSString *month = [[self.monthList[monthRow] stringByReplacingOccurrencesOfString:@"第" withString:@""] stringByReplacingOccurrencesOfString:@"周" withString:@""];
    
563 564
    NSArray *array1 = [NSArray arrayWithObjects:year,month.length == 1 ?[NSString stringWithFormat:@"0%@",month]:month, nil];
    NSString *date1 = [array1 componentsJoinedByString:@"-"];
陈俊俊's avatar
陈俊俊 committed
565 566 567
   
    
    return date1;
陈俊俊's avatar
陈俊俊 committed
568 569 570 571
}
- (void)cancelClick{
    [self.delegate cancelTimeView];
}
572 573 574
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.delegate cancelTimeView];
}
陈俊俊's avatar
陈俊俊 committed
575
@end