How to have two UIPickerViews together in one ViewController?
- by 0SX
I'm trying to put 2 UIPickerViews together in one ViewController. Each UIPickerView has different data arrays. I'm using interface builder to link the pickers up. I know I'll have to use separate delegates and dataSources but I can't seem to hook everything up with interface builder correctly. Here's all my code:
pickerTesting.h
#import <UIKit/UIKit.h>
#import "picker2DataSource.h"
@interface pickerTestingViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>{
IBOutlet UIPickerView *picker;
IBOutlet UIPickerView *picker2;
NSMutableArray *pickerViewArray;
}
@property (nonatomic, retain) IBOutlet UIPickerView *picker;
@property (nonatomic, retain) IBOutlet UIPickerView *picker2;
@property (nonatomic, retain) NSMutableArray *pickerViewArray;
@end
pickerTesting.m
#import "pickerTestingViewController.h"
@implementation pickerTestingViewController
@synthesize picker, picker2, pickerViewArray;
- (void)viewDidLoad
{
[super viewDidLoad];
pickerViewArray = [[NSMutableArray alloc] init];
[pickerViewArray addObject:@" 100 "];
[pickerViewArray addObject:@" 200 "];
[pickerViewArray addObject:@" 400 "];
[pickerViewArray addObject:@" 600 "];
[pickerViewArray addObject:@" 1000 "];
[picker selectRow:1 inComponent:0 animated:NO];
picker2.delegate = self;
picker2.dataSource = self;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker;
{
return 1;
}
- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component;
{
return [pickerViewArray count];
}
- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [pickerViewArray objectAtIndex:row];
}
- (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
{
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
And I have a separate class for the other datasource.
picker2DataSource.h
@interface picker2DataSource : NSObject <UIPickerViewDataSource, UIPickerViewDelegate>
{
NSMutableArray *customPickerArray;
}
@property (nonatomic, retain) NSMutableArray *customPickerArray;
@end
picker2DataSource.m
#import "picker2DataSource.h"
@implementation picker2DataSource
@synthesize customPickerArray;
- (id)init
{
// use predetermined frame size
self = [super init];
if (self)
{
customPickerArray = [[NSMutableArray alloc] init];
[customPickerArray addObject:@" a "];
[customPickerArray addObject:@" b "];
[customPickerArray addObject:@" c "];
[customPickerArray addObject:@" d "];
[customPickerArray addObject:@" e "];
}
return self;
}
- (void)dealloc
{
[customPickerArray release];
[super dealloc];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker2;
{
return 1;
}
- (void)pickerView:(UIPickerView *)picker2 didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (NSInteger)pickerView:(UIPickerView *)picker2 numberOfRowsInComponent:(NSInteger)component;
{
return [customPickerArray count];
}
- (NSString *)pickerView:(UIPickerView *)picker2 titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [customPickerArray objectAtIndex:row];
}
@end
Any help or code examples would be great. Thanks.