How to display the textfield value in picker selected row in iPhone? - iphone

I have an application where I have a pickerview. In that picker I am displaying values from 1 to 100. When I select a value in picker say 2 that value is displayed in a textfield. I want that if my textfield contains value say 5 that value particular value should get displayed in my picker.
Now problem is when in textfield 5 is displayed and I go to pickerview it shows by default every time 1 selected irrespective of the value in the texfield. I want the value which is present in the textfield should get displayed on the pickerview also.

Use below code it will help you
[picker selectRow:[yourTxtObj.text intValue]-1 inComponent:0 animated:YES];

[yourPickerViewName selectRow:[textfield.text intValue] inComponent:yourComponent animated:YES]
This will work. It is used for select a row in picker.

Related

iOS 5 Picker View update based on selection?

I am working on a project where I want to have a picker view with 3 columns.
In the first column I want the user to select a letter.
Once the letter is selected I want the second column to update with a list of last names that start with that letter. When a last name is selected I want the third column to update with a list of first names who have the selected last name.
Can this even be done? Help!?
Get the letter that they selected using this pickerview delegate method:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
You could load the next picker at this point but it would be better to populate the next picker when the done button on each picker is selected:
This is a nice SO post that describes how to use picker views very well: Dismissing UIPickerView with Done button on UIToolBar

How select value initally when picker view appear?

I have set a picker view appearance on text field begin editing function and hide keyboard. Now problem is that when picker view appear then it show selection indicator on first value but that value not show in text field. I want that when user click on text filed then picker view appear and value from picker will show in text field where selection indicator place. How do that? Suppose i have four value in same order 0,1,2,3. Now when picker view appear then selection indicator is at on '0' value. But to get that value we have to scroll picker view and come back to '0' then it show that value in text field. Why it is occurs? And how get value automatically?
Thanks in advances...
Well, there is no connection between your picker view and the text field - you have to make it yourself.
There are two things to consider:
1) Your text field already contains a value, let's say "1". When you open the picker, you can set it to this value already with the function
[yourPicker selectRow:1 inComponent:0 animated:false]
, assuming your data array for the picker is [0, 1, 2, 3]. In your case, to make the text field contain the value 0, just call
[textField setText:#"0"]
2) When you are finished with the picker, it calls the delegate function
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
So you have to make your view implement the UIPickerViewDelegate protocol, implement the above delegate function in it and set the view as the picker view's delegate.
In the delegate function you can get the value the user picked (using row to access your data array values) and set this value in your textfield.

saving text data from UITextField without pressing additional key (help needed)

I am new to iPhone programming. I am working with UITextField and UITextFieldDelegate. I have five textfields. I am trying to input data in them using numberPad keyboard. And the data should be 1 digit long. After that hitting another key should take it to the next textField. I have implemented it all but the only problem is when all 5 textfields are filled, I have to press keyboard 6th time to save the text data of fifth textField, Is there any way I could save the text data of 5th textfield without pressing any key on the keyboard 6th time?
Any help or suggestions are welcomed.
Thanks
Vik
You can use textFieldDidChange delegate Method for 5th TextField and compare it like this.
if (textField==textField5 && [textField length]>0){
[textField resignFirstResponder];
}
I hope this will help.
Im assuming that you are using UITextFieldDelegate method to move to next text field. You can keep a count on how many text fields are entered and keep checking it:
if(count == num_of_textfields)
{
[textField resignFirstResponder];
//Call your method to process the input here.
}

iphone UIPickerView select row

I have my view that contains the UIPickerView...
I tried to make this code:
[self.picker selectRow:rowSelected inComponent:0 animated:true];
to select the row with index rowSelected, but this doesn't work...
I need to put this in viewdidload??
You do not call this method yourself. It is called by the system when a user selects a row in the pickerview. You should override the implementation of that method to do whatever you wish should happen when the user selects that row in that component.

iphone UIPicker default position

Is there a way to set a default position for a UIPicker? I mean, if the user has the option of selecting any number from 1 to 100 in the UIPicker, can I set it to automatically start at 50?
You can use selectRow:inComponent:animated: like this:
[somePickerView selectRow:49 inComponent:0 animated:NO]; // Zero-indexed, so row 49 is the row that says 50
You can fire it in the viewDidLoad method for the UIViewController managing the view which contains the UIPickerView.
(It could also be that you can specify the default value in Interface Builder, but I'm not sure and I didn't check...)