24/7 twenty-four seven

iOS/OS X application programing topics.

UICCalendarPickerの使い方

基本形

UICCalendarPicker *calendarPicker = [[UICCalendarPicker alloc] initWithSize:UICCalendarPickerSizeMedium];
[calendarPicker setDelegate:self];
[calendarPicker setSelectionMode:UICCalendarPickerSelectionModeMultiSelection];
[calendarPicker showInView:self.view animated:YES];
[calendarPicker release];
1. サイズを指定してインスタンスを作ります。
UICCalendarPicker *calendarPicker = [[UICCalendarPicker alloc] initWithSize:UICCalendarPickerSizeMedium];
2. デリゲートを設定します。
[calendarPicker setDelegate:self];
3. 選択モードを設定します。

単一選択: UICCalendarPickerSelectionModeSingleSelection
複数選択: UICCalendarPickerSelectionModeMultiSelection
範囲選択: UICCalendarPickerSelectionModeRangeSelection

[calendarPicker setSelectionMode:UICCalendarPickerSelectionModeMultiSelection];

デリゲート・メソッド

ボタン(「閉じる」「先月」「来月」「日付」)を押したとき、対応したメソッドが呼ばれます。

@protocol UICCalendarPickerDelegate<NSObject>
@optional
- (void)picker:(UICCalendarPicker *)picker pushedCloseButton:(id)sender;
- (void)picker:(UICCalendarPicker *)picker pushedPrevButton:(id)sender;
- (void)picker:(UICCalendarPicker *)picker pushedNextButton:(id)sender;
- (void)picker:(UICCalendarPicker *)picker didSelectDate:(NSArray *)selectedDate;
@end

データソース・メソッド

日付の属性によって、外観をカスタマイズします。

@protocol UICCalendarPickerDataSource<NSObject>
@optional
- (NSString *)picker:(UICCalendarPicker *)picker textForYearMonth:(NSDate *)aDate;
- (void)picker:(UICCalendarPicker *)picker buttonForDateToday:(UICCalendarPickerDateButton *)button;
- (void)picker:(UICCalendarPicker *)picker buttonForDateWeekday:(UICCalendarPickerDateButton *)button;
- (void)picker:(UICCalendarPicker *)picker buttonForDateSaturday:(UICCalendarPickerDateButton *)button;
- (void)picker:(UICCalendarPicker *)picker buttonForDateSunday:(UICCalendarPickerDateButton *)button;
- (void)picker:(UICCalendarPicker *)picker buttonForDateMonthOut:(UICCalendarPickerDateButton *)button;
- (void)picker:(UICCalendarPicker *)picker buttonForDateOutOfRange:(UICCalendarPickerDateButton *)button;
- (void)picker:(UICCalendarPicker *)picker buttonForDateSelected:(UICCalendarPickerDateButton *)button;
- (void)picker:(UICCalendarPicker *)picker buttonForDateBlank:(UICCalendarPickerDateButton *)button;
- (void)picker:(UICCalendarPicker *)picker buttonForDate:(UICCalendarPickerDateButton *)button;
@end


例えば、土曜日だけ別のボタンにするには次のようにします。

- (void)picker:(UICCalendarPicker *)picker buttonForDateSaturday:(UICCalendarPickerDateButton *)button {
	[button setBackgroundImage:[UIImage imageNamed:@"uiccalendar_cell_custom1.png"] forState:UIControlStateNormal];
}

iPhone用日付選択UIライブラリ、UICCalendarPickerを作りました。 - 24/7 twenty-four seven