0

CoCoa UIDatePicker から日付を選択するようにユーザーに促す必要がありますが、日曜日と土曜日を選択しないようにする必要があります。私の目標は、予定の日付を選択させることだからです。

最善の方法は、minimumDate プロパティと同じ方法でその日付を無効にすることですが、その方法を見つけることができませんでした。

4

1 に答える 1

3

次のようなことができます。

UIDatePicker *datePicker = [[UIDatePicker alloc] init];
[datePicker addTarget:self action:@selector(dateChanged:) forControlEvent:UIControlEventValueChanged];

の実装dateChanged:

- (void)dateChanged:(id)sender {
  UIDatePicker *datePicker = (UIDatePicker *)sender;
  NSDate *pickedDate = datePicker.date;

  NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:pickedDate];
  NSInteger weekday = [weekdayComponents weekday];
  [gregorian release];

  if (weekday == 1 || weekday == 7) { // Sunday or Saturday
    NSDate *nextMonday = nil;
    if (weekday == 1)
      nextMonday = [pickedDate dateByAddingTimeInterval:24 * 60 * 60]; // Add 24 hours
    else
      nextMonday = [pickedDate dateByAddingTimeInterval:2 * 24 * 60 * 60]; // Add two days

    [datePicker setDate:nextMonday animated:YES];

    return;
  }

  // Do something else if the picked date was NOT on Saturday or Sunday.
}

このように、土曜日または日曜日のいずれかの日付が選択されると、日付ピッカーは週末の後の月曜日を自動的に選択します。

(コードはテストされていません!)

于 2010-07-03T12:22:38.143 に答える