[ios]在一个 ViewController 的三个 UIPickerViews

标签: Xcode iphone ios
发布时间: 2017/3/28 22:13:24
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有三个 UIPickerViews,我在一个 ViewController 中使用。它需要独立于彼此,因为我需要保存结果从每个,但在运行它时它看到三个 pickerView,d 相同的结果也保存,当你选择不同的 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 工作。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

解决方法 1:

您不需要实现 3 不同 (int) pickerView:(UIPickerView *) pickerView numberOfRowsInComponent: (NSInteger) 组件。我假设你有 3 向应造就为 UIPickerView,当用户点击文本字段的文本框。你也不需要 3 不同 UIPickerViews

你将需要有三个布尔常量 Bool firstTextFieldActive,Bool secondTextFieldActive Bool thirdTextFieldActive 设置所有这些为默认值"否"在 viewDidLoad。

一旦用户点击一个特定的文本字段设置为"YES"及其各自的布尔值

例如︰ 让我们说用户敲"最大",这使编译器调用"(BOOL) textFieldShouldBeginEditing:(UITextField *) 文本字段",然后在这里你将检查正在编辑的文本字段用下面的代码

-(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];
    }
}

希望这有助于

赞助商