我正在尝试将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


任何帮助或代码示例都很棒。谢谢。

最佳答案

使用Interface Builder,将两个UIPickerViews的标记属性分别设置为1和2。然后,在每个委托方法中,使用if语句检查UIPickerView参数的标记。

关于iphone - 如何在一个ViewController中将两个UIPickerView在一起?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4632182/

10-10 22:24