gpt4 book ai didi

ios - 一个 ViewController 中的三个 UIPickerView

转载 作者:行者123 更新时间:2023-11-28 19:55:22 25 4
gpt4 key购买 nike

我有三个 UIPickerView,我在一个 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

最佳答案

被调用的委托(delegate)和数据源方法对于所有选择器 View 都是相同的。不同之处在于传递给该方法的 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;
}
}

以此类推所有其他委托(delegate)/数据源方法。

关于ios - 一个 ViewController 中的三个 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26854172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com