3

我正在使用 textFields inputView 属性来显示 UIPickerViews 而不是键盘。按照我的编程方式,我没有在 .H 文件中声明 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,只要我能做到这一点,我不在乎我必须做什么工作!!

谢谢

更新!!!

   @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];
    }
}
4

2 回答 2

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

您需要在 .h 文件(或接口)中声明它。

你是子类化UIPickerView吗?

编辑*如果您出于某种原因不想在 .h 文件中声明它们,请将 a 设置tag为您的选择器。

您所有的UIPickerViews 都将调用 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
于 2012-07-29T05:00:29.940 回答
1

为每个选择器视图设置标签并根据标签以不同方式处理它们

于 2012-07-30T05:58:52.007 回答