How can I get text on UIPickerView? - iphone

If I use a UIPickerView with custom View(3 components) .How could I get each text on UIPickerView from each selectedRow??
THANKS...

You can use picker's delegate method
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component==0)
{
int i=row; //here i returns the row selected by you in picker component 0
}
else if(component==1)
{
int j=row; //here j returns the row selected by you in picker component 1
}
else if(component==2)
{
int k=row; //here k returns the row selected by you in picker component 2
}
}

It depends on how you decide to place the text into the components.
For example, if you use myArray:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"%#",[myArray objectAtIndex:row]);
}

Related

How to show previous selected value of a uipickerview in ios programming

How to show previously selected value of a UIPickerView in iOS programming
You have to take two variable for it 1) currentSelectedIndex 2)prevSelectedIndex
And In
- (void)pickerView:(UIPickerView *)pV didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
prevSelectedIndex = currentSelectedIndex
currentSelectedIndex = row;
//your Code...
}
now you can use prevSelectedIndex to get pickerDataSourceArray value which was previously selected.

Set UITextField's text as default value in UIPickerView in 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.

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 to place the morethan one pickerviews in view?

Hai,In my app i want to place the 4 pickerviews.And i place the one segment control with 3 segments.If we click on first segment then firstpicker will be display.Likewise remaining pickers are display for remaining segments.But,only first picker will be displayed and remaining pickers are not show components and rows.Only display black screen.Please provide any help.Please urgent.
To again call the delegates, call the following line
[pickerview reloadComponent];
create outlet and make use of hidden property...means if you tap on first segment make first picker as highlighted and similarly for other segments with respective pickers...
do you want to resize ??
Edit: added code from below for formatting purposes
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return ( pickerView == picker1 ? 2 : 3 );
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { NSArray *values = ( pickerView == picker1 ? values1 : values2 );
return [values count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSArray *values = ( pickerView == picker1 ? values1 : values2 );
return [values objectAtIndex: row];
}
check like this..