3단 헤테로 피커뷰 소스코드
// h --------------------------------------
#import! <UIKit/UIKit.h>
#define groupComponent 0
#define memberComponent 1
#define countComponent 2 //
@interface pick3ViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
{
UIPickerView *giveMePicker;
NSDictionary *giveMeList;
NSArray *groupName;
NSArray *memberName;
NSArray *countType; //
}
@property (nonatomic, retain) IBOutlet UIPickerView *giveMePicker;
@property (nonatomic, retain) NSDictionary *giveMeList;
@property (nonatomic, retain) NSArray *groupName;
@property (nonatomic, retain) NSArray *memberName;
@property (nonatomic, retain) NSArray *countType; //
- (IBAction)buttonPressed:(id)sender;
@end
// m ----------------------------------
#import! "pick3ViewController.h"
@implementation pick3ViewController
@synthesize giveMePicker, giveMeList, groupName, memberName, countType;
#pragma mark -
#pragma mark 화면 이벤트 처리
//!!!: 결과버튼 클릭시, 피커의 선택된 항목 경고창에 표시
- (IBAction)buttonPressed:(id)sender {
//NSInteger groupRow = [giveMePicker selectedRowInComponent:groupComponent];
NSInteger memberRow = [giveMePicker selectedRowInComponent:memberComponent];
NSInteger countRow = [giveMePicker selectedRowInComponent:countComponent]; //
//NSString *group = [self.groupName objectAtIndex:groupRow];
NSString *member = [self.memberName objectAtIndex:memberRow];
NSString *count = [self.countType objectAtIndex:countRow]; //
// 알림창용 제목
NSString *title = [[NSString alloc] initWithFormat:@"여기요"];
// 알림창용 내용
NSString *message = [[NSString alloc] initWithFormat:@"%@ %@병 주세요!", member, count];
// 알림창
UIAlert!View *alert! = [[UIAlert!View alloc] initWithTitle:title message:message
delegate:nil cancelButtonTitle:@"확인" otherButtonTitles:nil];
[alert! show];
[alert! release];
[title release];
[message release];
}
#pragma mark -
#pragma mark 초기화
//!!!: 뷰 기동후, 초기화
- (void)viewDidLoad {
NSArray *countArray =[[NSArray alloc] initWithObjects:@"1",@"2", @"3",
@"4",@"5", nil];
self.countType = countArray;
[countArray release];
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"GiveMe" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.giveMeList = dictionary;
[dictionary release];
NSArray *components = [self.giveMeList allKeys];
NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
self.groupName = sorted;
NSString *selectedGroup = [self.groupName objectAtIndex:0];
NSArray *array = [giveMeList objectForKey:selectedGroup];
self.memberName = array;
[super viewDidLoad];
}
#pragma mark -
#pragma mark 피커뷰 메써드 오버라이드
//!!!: 전체 단 수 리턴
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
//!!!: 각 단별 항목 수 리턴
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
if (component == groupComponent) {
return [groupName count];
} else if (component == memberComponent){
return [memberName count];
}else {
return [countType count];
}
}
//!!!: 각 항목별 항목명
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == groupComponent) {
return [groupName objectAtIndex:row];
} else if (component == memberComponent){
return [memberName objectAtIndex:row];
}else {
return [self.countType objectAtIndex:row];
}
}
//!!!: 1단 선택시 2단 애니메이션
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == groupComponent) {
NSString *selectedGroup = [self.groupName objectAtIndex:row];
NSArray *array = [giveMeList objectForKey:selectedGroup];
self.memberName = array;
[giveMePicker selectRow:0 inComponent:memberComponent animated:YES];
[giveMePicker reloadComponent:memberComponent];
}
}
#pragma mark -
#pragma mark 안전해제
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
//!!!: 뷰 언로드시
- (void)viewDidUnload {
self.giveMePicker = nil;
self.giveMeList = nil;
self.groupName = nil;
self.memberName = nil;
self.countType = nil;
}
- (void)dealloc {
[giveMePicker release];
[giveMeList release];
[groupName release];
[countType release];
[memberName release];
[super dealloc];
}
@end
|
댓글
댓글 쓰기