一个ViewController中有三个UIPickerView

时间:2014-11-10 22:26:37

标签: ios iphone xcode ipad viewcontroller

我有三个UIPickerViews,我在一个ViewController中使用它。它需要彼此独立,因为我需要保存每个的结果,但是当我运行它时,它会看到三个pickerView,当你选择不同的UIPickerView时,它也会保存相同的结果。 ViewController.m文件:

 @interface ViewController (){
 NSArray *_pickerData;
 NSArray *_pickerData2;
 NSArray *_pickerData3;

__weak IBOutlet UITextField *max;
__weak IBOutlet UITextField *m2;
__weak IBOutlet UITextField *m3;
 }
@end

@implementation ViewController


   - (void)viewDidLoad {
     [super viewDidLoad];
 _pickerData = @[@"Apple",@"Avocado",@"Asparagus",@"Grapefruit"];
    _pickerData2 = @[@"Apple",@"Avocado",@"Asparagus",@"Grapefruit"];
    _pickerData3 = @[@"Apple",@"Avocado",@"Asparagus",@"Grapefruit"];
// Do any additional setup after loading the view, typically from a nib.
self.picker.dataSource = self;
self.picker.delegate = self;
self.picker2.dataSource = self;
self.picker2.delegate = self;
self.picker3.dataSource = self;
self.picker3.delegate = self;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
 {
return 1;
 }

 // The number of rows of data
 - (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
 {
return _pickerData.count;
  }
  - (int)picker2View:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  {
return _pickerData2.count;
  }
  - (int)picker3View:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  {
return _pickerData3.count;
  }
 - (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:           (NSInteger)component                    
   {
   self->max.text = [NSString stringWithFormat:@"%@", _pickerData[row]];

    return _pickerData[row];

    }
   - (NSString*)pickerView2:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component
     {
     self->m2.text = [NSString stringWithFormat:@"%@", _pickerData2[row]];
     return _pickerData2[row];
      }
  - (NSString*)pickerView3:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:   (NSInteger)component
      {
       self->m3.text = [NSString stringWithFormat:@"%@", _pickerData2[row]];
       return _pickerData3[row];
       }
       @end

当我运行它时,三个PickerView像one.ViewController.h文件一样工作:

       #import <UIKit/UIKit.h>

      @interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>
      @property (weak, nonatomic) IBOutlet UIPickerView *picker;
      @property (weak, nonatomic) IBOutlet UIPickerView *picker2;
      @property (weak, nonatomic) IBOutlet UIPickerView *picker3;
      @end

3 个答案:

答案 0 :(得分:2)

对于所有选择器视图,被调用的委托和数据源方法都是相同的。区别在于传递给方法的pickerView参数。所以,例如,而不是:

- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;
}
- (int)picker2View:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData2.count;
}
- (int)picker3View:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData3.count;
}

你应该:

- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView == self.picker) {
        return _pickerData.count;
    } else if (pickerView == self.picker2) {
        return _pickerData2.count;
    } else if (pickerView == self.picker3) {
        return _pickerData3.count;
    }
}

等等所有其他委托/数据源方法。

答案 1 :(得分:1)

您不需要实现3个不同的(int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)组件。我假设您有3个文本字段,当用户点击TextField时,应显示UIPickerView。您不需要3个不同的UIPickerView

你将需要有三个布尔值Bool firstTextFieldActive,Bool secondTextFieldActive,Bool thirdTextFieldActive,将所有这些设置为默认值&#34; NO&#34;在viewDidLoad。

一旦用户点击特定的TextField,将其相应的布尔值设置为&#34; YES&#34;

例如:假设用户点击&#34; max&#34;,然后这使编译器调用&#34;(BOOL)textFieldShouldBeginEditing:(UITextField *)textField&#34;,在这里你将检查哪个正在使用以下代码编辑文本字段

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if([textField isEqual: max]){

        //First textfield is being edited
        firstTextFieldActive = YES;
        //Make sure the other textfield actives are set to NO
        secondTextFieldActive = NO;
        thirdTextFieldActive = NO;
        //Now reload pickerView
        [picker reloadAllComponents];
    }
}

在你的

之后
-(int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if(firstTextFieldActive){
        return _pickerData.count;
    }else if(secondTextFieldActive){
        return _pickerData2.count;
    }else if(thirdTextFieldActive){
        return _pickerData3.count;
    }
}

也只能实现其中一个

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component                    
{
    if(firstTextFieldActive){
        self->max.text = [NSString stringWithFormat:@"%@", _pickerData[row]];

        return _pickerData[row];
    }else if(secondTextFieldActive){
        self->m2.text = [NSString stringWithFormat:@"%@", _pickerData2[row]];
        return _pickerData2[row];
    }else if(thirdTextFieldActive){
        self->m3.text = [NSString stringWithFormat:@"%@", _pickerData2[row]];
        return _pickerData3[row];
    }
}

希望这有助于

答案 2 :(得分:0)

您重复了委托方法 - 这不是委托方法的工作方式。 上面实际工作的6种方法中只有一种是每组中的第一种 - 这些是UIPickerView将调用的实际委托方法。其他人永远不会受到打击。这就是为什么他们似乎都表现得相同。

传递给委托方法的pickerView参数已经告诉你它是3中的哪一个。 您需要通过将传递给方法的方法与您在viewController的.h中声明的@property进行比较来获取pickerView。此外,他们可能不需要在.h。

 //The following is the only one of the 3 "titleForRow" methods that's working
 - (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:           (NSInteger)component                    
   {
   //...result is that they're all set to the same array value Don't use the following line.
   //self->max.text = [NSString stringWithFormat:@"%@", _pickerData[row]];

  //use this instead to determine which UIPicker the argument `pickerView` refers to
  if (pickerView == self.picker) {
    return _pickerData[row]; //this assumes the data array has an NSString value for this row. What if it doesn't?  
   } else if ... //looks like pbasdf already mentioned this approach above... Account for the 3 options

   return nil; //after all the if/else branches, in case pickerView or row are nil, since you have to return something  
}


- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
 {
     //check the "pickerView" argument against your @properties. @pbasdf beat me to it! 
    //Same if/else check as the other delegate methods
    //then return the correct _pickerData.count; //
  }