Guest User

Untitled

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. -(NSInteger)numberOfComponentsInPickerView: (UIPickerView *)thePickerView
  2. { return 1; }
  3.  
  4. -(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent: (NSInteger)component
  5. {return [list count]; }
  6.  
  7. -(NSString *)pickerView:(UIPickerView *)thePickerview titleForRow:(NSInteger)row forComponent:(NSInteger)component
  8. {return [list objectAtIndex:row]; }
  9.  
  10. -(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent: (NSInteger)component
  11. {
  12. uitextfield1.text = [list objectAtIndex:row];
  13. utitextfield2.text = [list objectAtIndex:row];
  14. }
  15.  
  16.  
  17.  
  18. - (void)viewDidLoad {
  19.  
  20. [super viewDidLoad];
  21.  
  22. uitextfield1.inputView = pickerView;
  23. uitextfield2.inputView = pickerView;
  24.  
  25. list = [[NSMutableArray alloc] init];
  26. [list addObject:@"a"];
  27. [list addObject:@"b"];
  28. [list addObject:@"c"];
  29. [list addObject:@"d"];
  30. }
  31.  
  32. -(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent: (NSInteger)component
  33. {
  34. // textFields is an NSArray holding each of the textfields that are using the picker as an input view
  35. for (UITextField textField in textFields)
  36. {
  37. if ([textField isFirstResponder])
  38. {
  39. textField.text = [list objectAtIndex:row];
  40. break;
  41. }
  42. }
  43. }
  44.  
  45. #define kFirstTextField 101
  46. #define kSecondTextField 102
  47. #define kThirdTextField 103
  48. //... etc
  49.  
  50. [myPickerView setHidden:NO]; // or however you show the picker
  51. [myPickerView setTag:textField.tag];
  52. [myPickerView reloadAllComponents];
  53.  
  54. -(NSString *)pickerView:(UIPickerView *)thePickerview
  55. titleForRow:(NSInteger)row
  56. forComponent:(NSInteger)component
  57. {
  58. if (thePickerView.tag==kFirstTextField)
  59. { return [list1 count]; }
  60. if (thePickerView.tag==kSecondTextField)
  61. { return [anotherOrTheSameList count]; }
  62. // of course, you can also use a switch statement
  63. return [defaultList count];
  64. }
  65.  
  66. -(void) pickerView:(UIPickerView *)thePickerview
  67. didSelectRow:(NSInteger)row
  68. inComponent:(NSInteger)component
  69. {
  70. UITextField *field = (UITextField *) [thePickerview.superview viewWithTag:thePickerview.tag];
  71. // this assumes the UIPickerView is the subview of the same view as the UITextFields
  72. if (thePickerview.tag==kFirstTextField)
  73. { field.text = [list1 objectAtIndex:row]; }
  74. if (thePickerview.tag==kSecondTextField)
  75. { field.text = [anotherOrTheSameList objectAtIndex:row]; }
  76. // etc.
  77. // alternatively:
  78. field.text = [self pickerView:thePickerview titleForRow:row forComponent:0];
  79. }
Add Comment
Please, Sign In to add comment