今天犯了了個很低級的錯誤,想把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; }
} 以此總結,告誡自己,不要犯這些蛋疼的問題!