iOS 6.1での奇妙な動作このように、日付ピッカーの最小日付を現在の日付に設定しました
NSDate *currentTime = [NSDate date];
[picker setMinimumDate:currentTime];
しかし、アプリを実行すると、過去の日付までスクロールできますが、選択されていなくても、ピッカーは現在の日付に戻りません。これはiOS6.1バージョンでのみ発生し、残りのピッカーは正常に動作しています。
iOS 6.1での奇妙な動作このように、日付ピッカーの最小日付を現在の日付に設定しました
NSDate *currentTime = [NSDate date];
[picker setMinimumDate:currentTime];
しかし、アプリを実行すると、過去の日付までスクロールできますが、選択されていなくても、ピッカーは現在の日付に戻りません。これはiOS6.1バージョンでのみ発生し、残りのピッカーは正常に動作しています。
I got the same issue as you and fixed it with only setting the date to the maximum date manually (in this case I set the limit to the current date):
- (IBAction)pickerValueChanged:(id)sender {
dispatch_async(dispatch_get_main_queue(), ^{
UIDatePicker *datePicker = (UIDatePicker *)sender;
if ([self.datePicker.date compare:[NSDate date]] == NSOrderedDescending) {
datePicker.date = [NSDate date];
}
});
}
This function is triggered when the date value from the date picker did change. you can set a maximum or minimum value here.
次のように最小日付と最大日付を設定する必要があります。
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMinimumDate:minDate];
このコードを試してください
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setYear:30];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-30];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maxDate];
[datePicker setMinimumDate:minDate];