Set UITextField's text as default value in UIPickerView in iPhone.? - iphone

I have some text in textField on view load.I want that particular text to be set as default value of UIPickerView ..The value in textField is one of the value of UIPickerView..How can I make it possible.For the code I m writing now is always first value is selected as default value in UIPickerView.But I want the text of textField to be set as default value in PickerView.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
txtstate.text= [[arr objectAtIndex:row] valueForKey:#"Code"];
}
How can I do it ?

uint selectedRow = [pickerView selectedRowInComponent:0];
NSString *title = [[pickerView delegate] pickerView:myPickerView titleForRow:selectedRow inComponent:0];
If you have more than a single component, adjust accordingly.

Related

How to get the row number of the selected value of UIPickerView

I have a UIPickerView in my viewController. I can get the value of the row number of the selected value in the picker from the delegate method
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
But in my case, I need to get the row number of the selected component as soon as the view appears. Is there a way to get the row number/trigger the delegate method as soon as the view appears.
You just have to use selectedRowInComponent
selectedRowInComponent:
Returns the index of the selected row in a given component.
- (NSInteger)selectedRowInComponent:(NSInteger)component Parameters
component
A zero-indexed number identifying a component of the picker view.
Return Value
A zero-indexed number identifying the selected row, or -1 if no row is
selected. Availability
Available in iOS 2.0 and later.
See Also
– selectRow:inComponent:animated:
Related Sample Code
iPhoneCoreDataRecipes
Declared In UIPickerView.h
you use this for select in pickerview
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *str= [yardsArray objectAtIndex:row];
}
use this code when view will load
-(void)ViewDidLoad
{
NSString *str= [yardsArray count];
}
in viewDidLoad
travers the array of your objects and match object of which you want to find out the row
and tell me if this is the solution

How to change the picker rows based on another picker's selection?

I have two pickers - country and region. I need to populate the region picker based on the row selected in the country picker. I have arrays to populate both the pickers. I need some help to change the rows of region picker based on the country picker's selection. Any help would be greatly appreciated. Thanks a lot in advance.
Ok You have two pickers lets say countryPicker and regionPicker.
in the delegate method for UIPickerView add one condition
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *pickerTitle = #"";
if (pickerView == countryPicker)
{
pickerTitle = [countryFeeds objectAtindex:row];
//assigns the country title if pickerView is countryPicker
}
else if (pickerView == regionPicker)
{
pickerTitle = [regionFeeds objectAtindex:row];
//assigns the region title if pickerView is regionPicker
}
return pickerTitle;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView == countryPicker)
{
//selects the region corresponding to the selected country.
//totalRegions is a NSDictionary having country names as keys and array of their regions as values
regionFeeds = [totalRegions objectForKey:[countryFeeds objectAtindex:row]];
//Now reloading the regionPicker with new values.
[regionPicker reloadAllComponents];
}
else if (pickerView == regionPicker)
{
// your code to select a region
}
}
I hope this solves your problem :)
BR, Hari
Are they separate picker views, or one picker view with two columns?
Either way, when you change the value in one picker, just call reloadAllComponents on the other picker (or reloadComponent: if you are using a split picker) and then when it reloads the data from it's data source you can use the value from the first picker to supply the correct region list.
My suggestion is please maintain the country and region in one picker with two components(country,region).If u do like this then we can change the one component values based on another component value.
NSUInteger selectedRow= [CountryPicker selectedRowInComponent:0];
NSString *userchoice =[self pickerView:CountryPicker titleForRow:selectedRow forComponent:0];
This will give you the title for the selected row by the user in country picker
then using the arrays you have to populate names of regions
You can use
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
on the RegionPicker

How fixed tag value for text field of custom cell in table view?

I am implementing a tab;e in which i have created a custom cell for cell of tableview. On custom cell i am displaying two text field , one button and one image view. Now i want implement an event on text field. That is when i click on text field then appear picker view. With the help of picker view we can change text of text field. I have applied event like as when we click on text filed then appear a picker view. But when i scroll picker view then text field value not changing. so what i will do so that it happens?
In table view i have 10 rows and i want to insert value from single picker view. I have also set tag on text field of custom cell by using this code.
cell_object.txt_time.tag=[indexpath.row];
for selection value from picker view i use this code...
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
if (pickerView == myPicker)
{
if(cell_object.txt_time.tag==0)
{
[cell_object.txt_time setText:[NSString stringWithFormat:#"%#",[array_time objectAtIndex: [pickerView selectedRowInComponent:0]]]];
}
else if(cell_object.txt_time.tag==1)
{
[cell_object.txt_time setText:[NSString stringWithFormat:#"%#",[array_time objectAtIndex: [pickerView selectedRowInComponent:0]]]];
}
}
}
I have do for 10 rows text.tag. But it is not working.
What i do ?
Thanks in advance...
A simple implementation would be:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent(NSInteger)component{
NSString *string = [[NSString alloc] initWithFormat:#"%#",[myArray objectAtIndex:row]];
textfield.text = string;
}
myArray is the datasource for your UIPickerView. With the above code you will be able to tweak it to what you needed to do.

Need Help Setting UIPicker's Label From Selection

I have a UIPicker with a UILabel above it that updates displaying the current selection.
When I go to this view the 2nd time, the UIPicker's selection is on the last cell where I left off, but the label is on the default string. How can I make it so the label is also on where I last left off?
This is the code I am using:
- (void)displayPicker
{
self.numberLabel.text = #"1 number";
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.numberButtonText = [NSString stringWithFormat:#"%# number", [self.pickerArray objectAtIndex:row]];
self.numberLabel.text = self.numberButtonText;
}
If i assume correct, you are calling
-(void)displayPicker
to set the default numberLabel text value (as in self.numberLabel.text = #"1 number";).
Possible cause would be,
that - (void)displayPicker is called every time, the view is presented to the user, but
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
is not.
You might want to check for such behavior, by inserting some NSLog() statements and perhaps correct it by using something like this:
- (void)displayPicker
{
self.numberLabel.text = [NSString stringWithFormat: #"%i number",
[self.numberPicker selectedRowInComponent: 0 ]];
}

label not updating with picker view

I have a pickerview pulling from a datasource. I have code to update a label in the didSelectRow function, but the label is not updating. When I print the value to the NSLog, the proper value is printed. Is there something special I need to do to hookup the label so that it updates when the didSelectRow is eneter?
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (pickerView.tag == TagLensPicker){
[self lensArrayData];
label.text = [NSString stringWithFormat:#"%#",[description objectAtIndex:[pickerView selectedRowInComponent:0]]];
NSLog([NSString stringWithFormat:#"%#", [description objectAtIndex:[pickerView selectedRowInComponent:0]]]);
}
}
[pickerView selectedRowInComponent:0]
might be the source of your problem.
[description objectAtIndex:row] should work
If NSLog prints the correct value, there must be a problem with the label variable.
Print it using NSLog, see if it's a correct reference to your label?
Also, you may try calling [label setNeedsDisplay] after changing the text, although I'm not sure it's necessary.