今天犯了了個很低級的錯誤,想把UIDatePickView的默認選中時間設置為當前時間的下一天的11:30,我的思路:首先獲取當前時間---將當前時間字符串化-----截取次字符串,分理處當前的年月日中得日----將日+1----然後字符串拼接成自己想要的日期格式-------將拼接成的日期轉化成NSDate-------然後設置默認時間
我犯的錯誤,將字符串轉化為時間的時候,一直想象NSDate的會有這樣的方法,結果查看NSDate的API沒有查到。犯錯就是沒有第一時間想到:NSDateFormatter.
下面看看我設置的代碼:
UIDatePicker * pick = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-215, 320, 215)]; pick.minimumDate = [NSDate date]; NSDate * tempDate = [NSDate dateWithTimeIntervalSinceNow:24*60*60]; NSString * str = [NSString stringWithFormat:@"%@",tempDate]; NSString * result = [[str substringWithRange:NSMakeRange(0, 10)] stringByAppendingFormat:@" 11:30"]; NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSDate * resultDate = [formatter dateFromString:result]; [pick setDate:resultDate]; [pick addTarget:self action:@selector(pickValueChanged:) forControlEvents:UIControlEventValueChanged]; self.datePicker = pick; NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//設置為中文顯示 pick.locale = locale; [self.view addSubview:pick]; if (IPhone5) { self.bgView.frame = CGRectMake(0, -10, 320, self.view.frame.size.height); } else { self.bgView.frame = CGRectMake(0, -60, 320, self.view.frame.size.height); } #pragma mark - pick事件 -(void)pickValueChanged:(UIDatePicker *)aPick { if ([aPick.date timeIntervalSinceDate:[NSDate date]]<0) { NSDate * tempDate = [NSDate dateWithTimeIntervalSinceNow:24*60*60]; NSString * str = [NSString stringWithFormat:@"%@",tempDate]; NSString * result = [[str substringWithRange:NSMakeRange(0, 10)] stringByAppendingFormat:@" 11:30"]; NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSDate * resultDate = [formatter dateFromString:result]; [aPick setDate:resultDate]; } else { NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSString* dateStr = [dateFormatter stringFromDate:aPick.date]; NSString * tempStr = [dateStr substringToIndex:16]; self.text_time.text = tempStr; } } UIDatePicker * pick = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-215, 320, 215)]; pick.minimumDate = [NSDate date]; NSDate * tempDate = [NSDate dateWithTimeIntervalSinceNow:24*60*60]; NSString * str = [NSString stringWithFormat:@"%@",tempDate]; NSString * result = [[str substringWithRange:NSMakeRange(0, 10)] stringByAppendingFormat:@" 11:30"]; NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSDate * resultDate = [formatter dateFromString:result]; [pick setDate:resultDate]; [pick addTarget:self action:@selector(pickValueChanged:) forControlEvents:UIControlEventValueChanged]; self.datePicker = pick; NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//設置為中文顯示 pick.locale = locale; [self.view addSubview:pick]; if (IPhone5) { self.bgView.frame = CGRectMake(0, -10, 320, self.view.frame.size.height); } else { self.bgView.frame = CGRectMake(0, -60, 320, self.view.frame.size.height); } #pragma mark - pick事件 -(void)pickValueChanged:(UIDatePicker *)aPick { if ([aPick.date timeIntervalSinceDate:[NSDate date]]<0) { NSDate * tempDate = [NSDate dateWithTimeIntervalSinceNow:24*60*60]; NSString * str = [NSString stringWithFormat:@"%@",tempDate]; NSString * result = [[str substringWithRange:NSMakeRange(0, 10)] stringByAppendingFormat:@" 11:30"]; NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSDate * resultDate = [formatter dateFromString:result]; [aPick setDate:resultDate]; } else { NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSString* dateStr = [dateFormatter stringFromDate:aPick.date]; NSString * tempStr = [dateStr substringToIndex:16]; self.text_time.text = tempStr; }
} 以此總結,告誡自己,不要犯這些蛋疼的問題!