我现在的编码有问题。我想在一个视图中有两个选择器视图,每个选择器中的数据不同。因此,当我单击文本字段时,选择器会出现数据,当我单击另一个文本字段时,选择器会出现不同的数据。我设法创建了选择器,但相同的数据出现在每个文本字段中。我试图做一个分离选择器数据的操作,但我无法让它工作。
- (void)viewDidLoad{
dataArray = [[NSMutableArray alloc] init];
[dataArray addObject:@" "];
[dataArray addObject:@"IG"];
[dataArray addObject:@"G"];
[dataArray addObject:@"VG"];
[dataArray addObject:@"MVG"];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker;{
return 1;
}
- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
currentTextField.text = [dataArray objectAtIndex:row];
selectedText = currentTextField.text;
}
- (NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component;{
return [dataArray count];
}
- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component;{
return [dataArray objectAtIndex:row];
}
这是我用于选择器的代码,我试图为其他文本字段做一个精确的副本,但数据不同。
这是包含此代码的文本字段的操作:
- (IBAction)showPicker:(id)sender {
picker = [[UIPickerView alloc] init];
picker.showsSelectionIndicator = YES;
picker.dataSource = self;
picker.delegate = self;
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackTranslucent;
[toolbar sizeToFit];
//to make the done button aligned to the right
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:self
action:@selector(doneClicked:)];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
//custom input view
textField3.inputView = picker;
textField3.inputAccessoryView = toolbar;
textField5.inputView = picker;
textField5.inputAccessoryView = toolbar;
textField7.inputView = picker;
textField7.inputAccessoryView = toolbar;
textField9.inputView = picker;
textField9.inputAccessoryView = toolbar;
如何继续并将选择器中的其他数据连接到其他文本字段?