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

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.

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

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 ]];
}

how can I put particular action as I select any row of UIPickerView?

I am writing code like
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
int n=row+1;
if(row == 0)
return [NSString stringWithFormat:#"%d minute", n];
return [NSString stringWithFormat:#"%d minutes", n];
}
now I want to start timer as I choose any row, like row 5 has 5 mints, so I want to start timer with 5 mints, so what should I do, How can I choose particular row and press button and timer starts for that event?
For the future if you go onto google and type in the name of your class and "class reference" you can usually find the information you need on the apple website.
For instance, I just went onto google and typed "UIPickerView class reference" and got to here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPickerView_Class/Reference/UIPickerView.html.
On that website if you read the description it says "The delegate must adopt the UIPickerViewDelegate protocol" and if you click there it says, under tasks:
Responding to Row Selection –
pickerView:didSelectRow:inComponent:
This is the delegate method you need to implement that will get called when the user selects a row. If you click on it it gives a little description and such.
If you go to the description then the full method is:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
so when a user selects a row this method is called and the pickerView is passed into the method automatically.
You can then get the row that user chose. If you go back a page to the UIPickerView class reference you see this:
Selecting Rows in the View Picker
– selectRow:inComponent:animated:
– selectedRowInComponent:
The method selectedRowInComponent: is the one you want. So in the delegate method you can do something like this:
int selectedRow = (int)[pickerView selectedRowInComponent:0];
:) Thats what I do when I want to find answers to questions like this. :)
Edit:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
int selectedRow = (int)[pickerView selectedRowInComponent:0];
NSLog(#"%i",selectedRow);
}
Assuming you've implemented UIPickerViewDelegate protocol, here's Swift 5 Version of Thomas Clayson's answer:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let selectedRow = pickerView.selectedRow(inComponent: 0)
print(selectedRow)
}

How can I get text on UIPickerView?

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]);
}