Dependent Picker with 3 components

Discussion in 'iOS Programming' started by waffles123, May 27, 2009.

  1. waffles123 macrumors regular

    Feb 23, 2009
    Ok, so I'm trying to have an app with a custom date picker. Instead of the dates being: 1, 2, 3, 4, 5, etc, it would be: 1, 8, 15, 22, etc.

    This is the picker component I'm talking about: [​IMG]

    So, I thought I could do a dependent picker with 3 components, getting the data source from a plist. The problem is I only know how to do that with 2 components. Here's what I have: (state is the first component, zip is the second, and test is the third.)

    Here's my PickerViewController.h

    #import <UIKit/UIKit.h>
    #define kStateComponent	0
    #define kZipComponent 1
    #define kTestComponent 2
    @interface DependentComponentPickerViewController : UIViewController
    	<UIPickerViewDelegate, UIPickerViewDataSource>
    	IBOutlet UIPickerView *picker;
    	NSDictionary *stateZips;
    	NSArray *states;
    	NSArray *zips;
    	NSArray *tests;
    @property (nonatomic, retain) UIPickerView *picker;
    @property (nonatomic, retain) NSDictionary *stateZips;
    @property (nonatomic, retain) NSArray *states;
    @property (nonatomic, retain) NSArray *zips;
    @property (nonatomic, retain) NSArray *tests;

    Here's my PickerViewController.m

    #import "DependentComponentPickerViewController.h"
    @implementation DependentComponentPickerViewController
    @synthesize picker;
    @synthesize stateZips;
    @synthesize states;
    @synthesize zips;
    @synthesize tests;
    	NSInteger stateRow = [picker selectedRowInComponent:kStateComponent];
    	NSInteger zipRow = [picker selectedRowInComponent:kZipComponent];
    	NSInteger testRow = [picker selectedRowInComponent:kTestComponent];
    	NSString *state = [self.states objectAtIndex:stateRow];
    	NSString *zip = [self.zips objectAtIndex:zipRow];
    	NSString *test = [self.tests objectAtIndex:testRow];
    	NSString *title = [[NSString alloc] initWithFormat:@"You selected zip code %@.", zip];
    	NSString *message = [[NSString alloc] initWithFormat:@"%@ is in %@", zip, state];
    	UIAlertView *alert = [[UIAlertView alloc] 
    	[alert show];
    	[alert release];
    	[title release];
    	[message release];
    -(void)viewDidLoad {
    	NSBundle *bundle = [NSBundle mainBundle];
    	NSString *plistPath = [bundle pathForResource:@"statedictionary" ofType:@"plist"];
    	NSDictionary *dictionary = [[NSDictionary alloc]
    	self.stateZips = dictionary;
    	[dictionary release];
    	NSArray *components = [self.stateZips allKeys];
    	NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
    	self.states = sorted;
    	NSString *selectedState = [self.states objectAtIndex:0];
    	NSArray *array = [stateZips objectForKey:selectedState];
    	self.zips = array;
    - (void)dealloc {
        [picker release];
    	[stateZips release];
    	[zips release];
    	[tests release];
    	[super dealloc];
    #pragma mark -
    #pragma mark Picker Date Source Methods
    - (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView
    	return 3;
    - (NSInteger)pickerView:(UIPickerView *)pickerView
    	if (component == kStateComponent)
    		return [self.states count];
    	return [self.zips count];
    	return [self.tests count];
    #pragma mark Picker Delegate Methods
    - (NSString *)pickerView:(UIPickerView *)pickerView
    	if (component == kStateComponent)
    		return [self.states objectAtIndex:row];
    	return [self.zips objectAtIndex:row];
    	return [self.tests objectAtIndex:row];
    -(void)pickerView:(UIPickerView *)pickerView
    	if (component ==kStateComponent)
    		NSString *selectedState = [self.states objectAtIndex:row];
    		NSArray *array = [stateZips objectForKey:selectedState];
    		self.zips = array;
    		[picker selectRow:0 inComponent:kZipComponent animated:YES];
    		[picker reloadComponent:kZipComponent];
    		self.tests = array;
    		[picker selectedRow:0 inComponent:kTestComponent animated:YES];
    		[picker reloadComponent:kTestComponent];
  2. ybrikeeg macrumors newbie

    Feb 5, 2009
    How do you do it with two components?

    Hi there,
    I was wondering if you could tell me how to make a UIPicker with two dependent components. Your help would be great appreciated!!!

  3. icewing macrumors 6502

    Jun 19, 2008
    St Louis
    The example above is from Beginning iPhone 3 Development - the example in the book uses (United States) States and Zip Codes.

    I'm an experienced midrange programmer (IBM iSeries), new to iPhone development, and so far the book's been pretty good. They have a support site that's been very helpful in figuring out errors, too.
  4. ybrikeeg macrumors newbie

    Feb 5, 2009
    Dependent Picker displays Images

    Thanks, now I need the picker to display an image once a row is selected from the dependent component. How would I do that? I have literally searched everything on Google and have tried every thing. Your help is greatly appreciated.

Share This Page