问题
I have a problem with my coding right now. I want to have two pickerviews in one view with different data in each picker. So when I click on a textfield a picker would appear with data, and when i click on another textfield the picker appears with different data. I managed to create the picker but the same data appears in each textfield. I have tried to make an action that separates the data of the pickers but I can't manage to get it to work.
- (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];
}
This is the code I am using for the picker and I have tried to do an exact copy for the other textfields but with different data.
And here is the action for the textfields that includes this code:
- (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;
How can I proceed and connect other data from the picker to other textfields?
回答1:
Create 2 arrays--one for each picker view. Within these arrays, contain the options that the user should be able to choose from. This can be easily accomplished with NSArray.
Set up your textfields in Interface Builder (i.e. in your Storyboard file), and connect an IBAction from each textfield to a function within the view controller's code.
Within each of these functions, instantiate a new UIPickerView with the contents of the array corresponding to the current picker.
After the user has selected the item, set the textfield's content to the array value contained within the index selected by the picker.
Remove the picker from the current view.
来源:https://stackoverflow.com/questions/10917615/multiple-uipickerviews-in-one-view-with-multiple-textfields