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

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

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 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.

Multiple UIPickerView with same data in one view. How can I get the content of both picker?

I have 2 UIPickerviews with the same data.
At the moment sb is clicking the save-Button I want to get the data from both PickerViews.
I already set a tag to both pickers and I also implemented the function:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [symbolList objectAtIndex:row];
}
But I have no idea how to get on both datas at the same time.
For each picker view, use selectedRowInComponent: to find out the current selection. Use this to obtain the value from each picker view's data source, e.g:
NSString *pickerViewOneSelection = [symbolList objectAtIndex:[pickerViewOne selectedRowInComponent:0]];
NSString *pickerViewTwoSelection = [symbolList objectAtIndex:[pickerViewTwo selectedRowInComponent:0]];
I'm assuming that pickerViewOne and pickerViewTwo are pointers to your two picker views and you've already worked that part out. I have also assumed that your picker has only one component.
Set tag of pickerview.
first create IBOutlet of both picker view.
and set tag of picker view.
pickerview.tag == 10;

Fill UIPickerView with an Array Value

i am working on a project in which i have to perform following two work:
1.) Fetch value from CoreData and store it in an NSMutableArray.
2.) Take a UIPickerView and fill it with an Array value.
Problem is that size of array is dynamic and i canto fill an array value in UIPickerView. can someone help me.
in order for the UIPickerView to work correctly, you must supply the amount of components (columns) and the number of rows for each component whenever it reloads:
as mentioned, use [myPickerView reloadAllComponents]; to reload the view once the array is populated, but you MUST implement also these things after you declare the containing view controller class as <UIPickerViewDelegate> link the picker to the file owner as a delegate, and then:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;// or the number of vertical "columns" the picker will show...
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (myLoadedArray!=nil) {
return [myLoadedArray count];//this will tell the picker how many rows it has - in this case, the size of your loaded array...
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//you can also write code here to descide what data to return depending on the component ("column")
if (myLoadedArray!=nil) {
return [myLoadedArray objectAtIndex:row];//assuming the array contains strings..
}
return #"";//or nil, depending how protective you are
}
After updating the value (Inserting or modifying existing) in your array.
Call reloadAllComponents on your UIPickerView instance.
- (void)reloadAllComponents
Use as below
[myPickerView reloadAllComponents];

In iphone can i add one picker view on two text boxes /uilabel with diffrent values?

In my project i am filling the picker view array with the value which i am getting from the service.And in the same view i have another label on click of button in the label i want to get the same uipickerview with different value is that possible.....so with one picker view my work will be done no need to call to different picker views...anyone has solution for this isssue
i dont think so .. it will only be possible if you load the data again which means the uipicker(all same instance) with loaded with same data
of course this is possible. Just switch to another model that holds the data that is used in the picker.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
if (myCurrentEditingValue == MyCountryValue) {
return ...
}
else if (myCurrentEditingValue == MyCityValue) {
return ...
}
return nil;
}
- (IBAction)startEditingCountryField {
myCurrentEditingValue = MyCountryValue;
[picker reloadAllComponents];
}
You should get the idea.