-2

I am displaying text fields in a UIView dynamically, based on JSON response data using a for loop.

I need to show a UIPickerView for the each text field in the for loop, but it is only working for the last text field. I am unable to display a UIPickerView for each of the text fields. Can anyone please help me to solve this issue?

Paulw11
  • 108,386
  • 14
  • 159
  • 186
ananthi
  • 23
  • 5
  • 2
    Please clarify what you are asking and provide code that you have tried. – sschale Mar 10 '16 at 05:24
  • Please mention what you have done . – sourav Mar 10 '16 at 05:27
  • 1
    You donot need to add picker view in for loop instead use one and change the array for different scenerio. – Reshmi Majumder Mar 10 '16 at 05:27
  • Use the tag and UITextFiled Delegates for open Picker view on startEditing textfiled. There is no need to set pickerview in ForLoop – Nitin Gohel Mar 10 '16 at 05:27
  • create the pickerview in seperate at the same time assign the tag for each textfield , if you press the textfield, based on tag change the frame of pickerview and display , thats all – Anbu.Karthik Mar 10 '16 at 05:38
  • self.downPicker = [[DownPicker alloc] initWithTextField:self.selectField withData:listArrayValue[i]]; this code is used to setup the pickerview in textfield but it is coming only for the last textfield – ananthi Mar 10 '16 at 05:41
  • Actually am displaying textfield dynamically based on the json data,in that how to set a tag for the uitextfield could please explain in detail. – ananthi Mar 10 '16 at 05:55

2 Answers2

1

So you are inside your for loop with an i index. What we are looking here is to assign a UIPickerView as each textfield's input view. Try something like this...

//FOR LOOP BEGINS

  //Iteration - Create a textfield.
  [self.view addSubview:yourTextfield];

  //Assign a uipickerview as textfield's input view.  
  UIPickerView *pickerView = [[UIPickerView alloc] init];
  pickerView.frame = CGRectMake(0,0,300,300);
  pickerView.tag = i;
  pickerView.delegate = self;
  pickerView.showsSelectionIndicator = YES;
  textField.inputView = pickerView;
  [self.view addSubview:pickerView];

  //Iteration ends

//FOR LOOP ENDS

P.S. You can also add a toolbar with each pickerView to your textfield using the inputAccessoryView

Pulkit Sharma
  • 545
  • 1
  • 6
  • 19
  • If i add like above code am getting *** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]' error .Am adding listArray = ([listArrayValue objectAtIndex:i]; )this list array count in -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component – ananthi Mar 10 '16 at 06:18
0

You can set the number of rows to a large number, and make it start at a high value, there's little chance that the user will ever scroll the wheel for a very long time -- And even then, the worse that will happen is that they'll hit the bottom. you can see this: How do you make an UIPickerView component wrap around?

Community
  • 1
  • 1
shujucn
  • 145
  • 1
  • 5