Skip to content

Instantly share code, notes, and snippets.

@udonchan
Created March 17, 2010 04:38
Show Gist options
  • Save udonchan/334925 to your computer and use it in GitHub Desktop.
Save udonchan/334925 to your computer and use it in GitHub Desktop.
月と日だけを選択するピッカ.iPhone用.
#define _COMPONENT_MONTH_LABEL 0
#define _COMPONENT_DAY_LABEL 1
@implementation UIMonthAndDayPickerView
@synthesize month, day;
- (id)initWithFrame:(CGRect)aRect {
if ([super initWithFrame:aRect] != nil) {
self.delegate = self;
self.dataSource = self;
self.showsSelectionIndicator = YES;
}
month = day = 1;
return self;
}
- (void)dealloc {
[super dealloc];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if(component == _COMPONENT_MONTH_LABEL) {
return 12;
} else if (component == _COMPONENT_DAY_LABEL) {
switch([self selectedRowInComponent:_COMPONENT_MONTH_LABEL]){
case 1:
return 29;
case 3:
case 5:
case 8:
case 10:
return 30;
default:
return 31;
}
} else {
return 0;
}
}
- (NSString*)pickerView: (UIPickerView*) pView titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
switch (component) {
case _COMPONENT_MONTH_LABEL:
return [NSString stringWithFormat:@"%d 月 ", row+1];
case _COMPONENT_DAY_LABEL:
return [NSString stringWithFormat:@"%d 日", row+1];
default:
return @"";
}
}
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
[self reloadComponent:_COMPONENT_DAY_LABEL];
month = [self selectedRowInComponent:_COMPONENT_MONTH_LABEL] + 1;
day = [self selectedRowInComponent:_COMPONENT_DAY_LABEL] + 1;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment