Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(NSInteger)numberOfComponentsInPickerView: (UIPickerView *)thePickerView
- { return 1; }
- -(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent: (NSInteger)component
- {return [list count]; }
- -(NSString *)pickerView:(UIPickerView *)thePickerview titleForRow:(NSInteger)row forComponent:(NSInteger)component
- {return [list objectAtIndex:row]; }
- -(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent: (NSInteger)component
- {
- uitextfield1.text = [list objectAtIndex:row];
- utitextfield2.text = [list objectAtIndex:row];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- uitextfield1.inputView = pickerView;
- uitextfield2.inputView = pickerView;
- list = [[NSMutableArray alloc] init];
- [list addObject:@"a"];
- [list addObject:@"b"];
- [list addObject:@"c"];
- [list addObject:@"d"];
- }
- -(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent: (NSInteger)component
- {
- // textFields is an NSArray holding each of the textfields that are using the picker as an input view
- for (UITextField textField in textFields)
- {
- if ([textField isFirstResponder])
- {
- textField.text = [list objectAtIndex:row];
- break;
- }
- }
- }
- #define kFirstTextField 101
- #define kSecondTextField 102
- #define kThirdTextField 103
- //... etc
- [myPickerView setHidden:NO]; // or however you show the picker
- [myPickerView setTag:textField.tag];
- [myPickerView reloadAllComponents];
- -(NSString *)pickerView:(UIPickerView *)thePickerview
- titleForRow:(NSInteger)row
- forComponent:(NSInteger)component
- {
- if (thePickerView.tag==kFirstTextField)
- { return [list1 count]; }
- if (thePickerView.tag==kSecondTextField)
- { return [anotherOrTheSameList count]; }
- // of course, you can also use a switch statement
- return [defaultList count];
- }
- -(void) pickerView:(UIPickerView *)thePickerview
- didSelectRow:(NSInteger)row
- inComponent:(NSInteger)component
- {
- UITextField *field = (UITextField *) [thePickerview.superview viewWithTag:thePickerview.tag];
- // this assumes the UIPickerView is the subview of the same view as the UITextFields
- if (thePickerview.tag==kFirstTextField)
- { field.text = [list1 objectAtIndex:row]; }
- if (thePickerview.tag==kSecondTextField)
- { field.text = [anotherOrTheSameList objectAtIndex:row]; }
- // etc.
- // alternatively:
- field.text = [self pickerView:thePickerview titleForRow:row forComponent:0];
- }
Add Comment
Please, Sign In to add comment