如何在一个 ViewController 中同时拥有两个 UIPickerView?
我试图将 2 个 UIPickerViews 放在一个 ViewController 中。每个 UIPickerView 都有不同的数据数组。我正在使用界面生成器来链接选择器。我知道我必须使用单独的委托和数据源,但我似乎无法将所有内容与界面生成器正确连接起来。这是我的所有代码:
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
任何帮助或代码示例都会很棒。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Interface Builder,将两个 UIPickerView 的标记属性分别设置为 1 和 2。然后,在每个委托方法中,使用 if 语句检查 UIPickerView 参数的标签。
Using Interface Builder, set the tag properties of the two UIPickerViews to 1 and 2, respectively. Then, in each delegate method, use if statements to check the tag of the UIPickerView argument.
UIPickerViewDataSource
方法怎么样//这返回要显示的“列”数。
How about
UIPickerViewDataSource
method//This returns the number of 'columns' to display.