我正在尝试将2 UIPickerViews
放在一个ViewController
中。每个UIPickerView
都有不同的数据数组。我正在使用界面构建器来链接拾取器。我知道我将不得不使用单独的委托和dataSources,但我似乎无法正确地连接接口构建器。这是我的所有代码:
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
我有一个单独的类用于其他数据源。
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
任何帮助或代码示例都会很棒。感谢。
答案 0 :(得分:5)
使用Interface Builder,将两个UIPickerViews的标签属性分别设置为1和2。然后,在每个委托方法中,使用if语句来检查UIPickerView参数的标记。
答案 1 :(得分:1)
UIPickerViewDataSource
方法
//返回要显示的“列”数。
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}