如何在同一视图中编写多个UIPickerView?

时间:2012-07-29 04:50:50

标签: ios xcode uipickerview textfield inputview

我使用textFields inputView属性来显示UIPickerViews而不是键盘。我编程的方式,我没有在.H文件中声明PickerViews,或者在界面构建器的视图中连接它们。当我实现pickerviews的属性时,我无法使每个属性具有不同的属性。我尝试使用“if”语句告诉它具有不同的属性,具体取决于使用的选择器如下....

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)agePickerView {
    if ([pickerView isEqual: agePickerView]){
                 return 1;
    }
            if ([pickerView isEqual: weightPickerView]){
                 return 1;
    }

}

当我这样做时,它会说weightPickerView未声明。所以这是我实现pickerViews的代码。

@implementation MainViewController

//WEIGHT FIELD CODE 

-(IBAction)weightFieldDidBeginEditing{
UIPickerView *weightPickerView = [[UIPickerView alloc] init];
weightField.inputView = weightPickerView;
weightPickerView.showsSelectionIndicator = YES;
weightPickerView.delegate = self;
weightPickerView.dataSource = self;
[weightPickerView release];

UIToolbar *weightToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
[weightToolBar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
 weightField.inputAccessoryView = weightToolBar;
}

-(NSInteger)weightpickerView:(UIPickerView *)weightPickerView numberOfRowsInComponent: (NSInteger)component{
return [weightPickerArray count];
}

-(NSString *)weightpickerView:(UIPickerView*)weightPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [weightPickerArray objectAtIndex:row];
}

// AGE FIELD CODE

-(IBAction)ageFieldDidBeginEditing{
UIPickerView *agePickerView = [[UIPickerView alloc] init];
ageField.inputView = agePickerView;
agePickerView.showsSelectionIndicator = YES;
agePickerView.delegate = self;
agePickerView.dataSource = self;
[agePickerView release];

UIToolbar *ageToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
[ageToolbar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
ageField.inputAccessoryView = ageToolbar; 
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)agePickerView {
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)agePickerView numberOfRowsInComponent: (NSInteger)component {
return [agePickerArray count];
}

-(NSString *)pickerView:(UIPickerView*)agePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [agePickerArray objectAtIndex:row];

}

-(void)pickerView:(UIPickerView *)agePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    ageField.text = (NSString *) [agePickerArray objectAtIndex:row];
}

-(void)pickerDoneClicked:(id)sender{
[ageField resignFirstResponder];
[weightField resignFirstResponder];
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a    nib.
- (void)viewDidLoad {
[super viewDidLoad];

agePickerArray = [[NSMutableArray alloc] init];
for (int age=1; age<=100; age++) {
    NSString *ageString = [NSString stringWithFormat:@"%d%",age];
                           [agePickerArray    addObject:ageString];


weightPickerArray = [[NSMutableArray alloc] init];
for (int weight = 50; weight<=500; weight++) {
    NSString *weightString = [NSString stringWithFormat:@"%d%",weight];
                            [weightPickerArray   addObject:weightString];
    }
}

有没有其他方法可以做我想做的事情,因为这种方式不起作用...如果我只是尝试重做每个选择器视图的实现,我会得到“重新定义...的错误“”numberOfRowsInComponent“”或者我正在做的其他属性。请帮助,即使我必须更改它并以某种方式在.H文件中声明pickerViews,我也不在乎我必须做什么,只要我能让这个工作!!

感谢

UPDATE !!!

   @interface MainViewController : UIViewController   <FlipsideViewControllerDelegate,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource> {

IBOutlet UITextField*ageField;

NSMutableArray *agePickerArray;
IBOutlet UITextField*weightField;
NSMutableArray *weightPickerArray;
UIPickerView *weightPickerView;
UIPickerView *agePickerView;    

   }

- (IBAction)ageFieldDidBeginEditing;
- (IBAction)weightFieldDidBeginEditing;
- (IBAction)showInfo:(id)sender;
- (IBAction)activityButtonClicked;
@end

.m文件

-(IBAction)weightFieldDidBeginEditing{
//UIPickerView *weightPickerView = [[UIPickerView alloc] init];
weightField.inputView = weightPickerView;
weightPickerView.showsSelectionIndicator = YES;
weightPickerView.delegate = self;
weightPickerView.dataSource = self;
[weightPickerView release];

UIToolbar *weightToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
[weightToolBar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
 weightField.inputAccessoryView = weightToolBar;
}

-(NSInteger)weightpickerView:(UIPickerView *)weightPickerView numberOfRowsInComponent:(NSInteger)component{
return [weightPickerArray count];
}

-(NSString *)weightpickerView:(UIPickerView*)weightPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [weightPickerArray objectAtIndex:row];
}

//年龄字段代码

-(IBAction)ageFieldDidBeginEditing{
//UIPickerView *agePickerView = [[UIPickerView alloc] init];
ageField.inputView = agePickerView;
agePickerView.showsSelectionIndicator = YES;
agePickerView.delegate = self;
agePickerView.dataSource = self;
[agePickerView release];

UIToolbar *ageToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
[ageToolbar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
ageField.inputAccessoryView = ageToolbar; 
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)agePickerView {
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if ([pickerView isEqual: agePickerView]) {
    return [agePickerArray count];
}
if ([pickerView isEqual:weightPickerView]) {
    return [weightPickerArray count];
}
}

-(NSString *)pickerView:(UIPickerView*)agePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [agePickerArray objectAtIndex:row];

}

-(void)pickerView:(UIPickerView *)agePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    ageField.text = (NSString *) [agePickerArray objectAtIndex:row];
}

-(void)pickerDoneClicked:(id)sender{
[ageField resignFirstResponder];
[weightField resignFirstResponder];
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a   nib.
- (void)viewDidLoad {
[super viewDidLoad];

agePickerArray = [[NSMutableArray alloc] init];
for (int age=1; age<=100; age++) {
    NSString *ageString = [NSString stringWithFormat:@"%d%",age];
                           [agePickerArray   addObject:ageString];


weightPickerArray = [[NSMutableArray alloc] init];
for (int weight = 50; weight<=500; weight++) {
    NSString *weightString = [NSString stringWithFormat:@"%d%",weight];
                            [weightPickerArray addObject:weightString];
    }
}

2 个答案:

答案 0 :(得分:7)

UIPickerView *weightPickerView = [[UIPickerView alloc] init];

您需要在.h文件(或界面)中声明。

您是否将UIPickerView作为子类?

编辑*如果您由于某种原因不想在.h文件中声明这些内容,请将tag设置为您的选择器。

你的所有UIPickerView都将调用UIPickerViewDelegate方法,这意味着除非你是子类化并创建自己的方法(我相信你不是),否则不会调用-(NSInteger)weightpickerView:(UIPickerView *)weightPickerView numberOfRowsInComponent: (NSInteger)component,即

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
  if([pickerView isEqual: agePickerView]){
   return [agePickerArray count];
  }else if([pickerView isEqual: weightPickerView]){
   return [weightPickerArray count];
  }else return 0;
}

如果你想用标签来做;

weightPickerView.tag = 100; //set the tag

if(pickerView.tag == 100) //check which picker it is

答案 1 :(得分:1)

将标记设置到每个选择器视图,并根据标记

以不同方式处理它们