5

iOS 6.1での奇妙な動作このように、日付ピッカーの最小日付を現在の日付に設定しました

NSDate *currentTime = [NSDate date];
[picker setMinimumDate:currentTime];

ここに画像の説明を入力してください

しかし、アプリを実行すると、過去の日付までスクロールできますが、選択されていなくても、ピッカーは現在の日付に戻りません。これはiOS6.1バージョンでのみ発生し、残りのピッカーは正常に動作しています。

4

4 に答える 4

8

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.

于 2013-05-06T18:39:31.583 に答える
2

次のように最小日付と最大日付を設定する必要があります。

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];
于 2013-03-20T07:09:44.383 に答える
2

このコードを試してください

 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];
于 2013-03-20T07:11:04.133 に答える