how to place previously selected value at UIPickerview bar - iphone

I am displaying my array objects in a pickerview.
My array contains {one,two,three,four,five}
When I select a value in picker view, I am getting that value using
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
If I select two, exit from the picker view and for next time it display one in picker view bar.
But I need to display previously selected value.
I think this is understandable otherwise let me add comment.

you need to store the value of the selected row in a variable call it selectedRow and when the view appears use this method on the object of pickerView
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
selectedRow = row;
}
- (void)viewWillAppear
{
[picker selectRow:selectedRow inComponent:0 animated:YES];
}
to select the row in the particular component.

- (void) viewDidAppear:(BOOL)animated
{
[pickerView selectRow:selectedItem inComponent:0 animated:YES];
}

call method
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
After displaying it for second time

In case the pickerview was alloc/inited and then added as a subview at the view then
[picker selectRow:selectedRow inComponent:0 animated:YES];
works only after
[self.view addSubview:picker];

Related

Autoselect picker row

I've been looking for quite a while after an answer for this, but can't seem to fint the solution...
Whenever I open my picker I need to pull the screen a little bit in order for the first row to get "picked" otherwise it will return a blank string.
Is there a way to by default autoselect the first row so that you wont need to do this motion?
Thanks in advance
Use
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
to set any default value that you want.
You could use:
[pickerView selectRow:0 inComponent:0 animated:NO]
In your viewDidLoad method.
You just declare NSString in your .h file
NSString *strVal;
then in your viewDidLoad method assign your default value to the string
strVal=your default value;
and your delegate method :
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:
(NSInteger)component
{
strVal=assign element;
}

How to select/set a selected row in picker view as default selected row every time

I have a picker view
[picker selectRow:3 inComponent:0 animated:YES];
when i wrote this code in view did load,
This method is used to set the row 3 as default row in picker.
But i want that the row i have selected is set to be as default for the next time.
what to do? thanks in advance...
Try like this
NSUserDefaults *usrDefaults = [NSUserDefaults standardUserDefaults];
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[usrDefaults setInteger:row forKey:#"Index"];
}
[pickerView selectRow:[[NSUserDefaults standardUserDefaults]integerForKey:#"Index"] inComponent:0 animated:NO];
Why dont you save the last pressed row in a variable and you load it next time?
Something like:
int myLastPressed;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
myLastPressed = row;
}
You can use a variable in your code, or a global one in NSUSerDefaults
And then next time:
[picker selectRow:myLastPressed inComponent:0 animated:YES];
This question is very unprecise. What do you want to do?
selectRow:inComponent: is exactly what you have to call, if you want a specific row selected.
If you want to save the selected state for the next appstart, you could save the current selected row (read with selectedRowInComponent) in the UserDefaults: [[NSUserdefault standardUserDefaults] saveInteger: selectedRow forKey #"savedSelectedPickerRowComponent1"]

UIPickerView for Iphone

I am creating a unit convertor app using a pickerview with three components and a plist..first one is category component and second and third are unit components. What i need is when i click an item in category component the remaining two components should get the units regarding that particular category. Can some one help me on this?
this is for selecting the same row in each component
here i did with first component do like that for ur logic
so change this method and u have to implement UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (component == 1) {
[pickerView selectRow:row inComponent:1 animated:YES];
[pickerView selectRow:row inComponent:2 animated:YES];
}
}

Custom UIPickerView

I want to use custom picker view that will add a check sign in front of selected row.
I have used apple sample code for custom UIPickerView on the UICatalog example. I was able to add the check sign for a given row when creating the picker. But I failed to add it when the user rotate the wheel to select new row and also to remove it from the previously added row. Any help would be appreciated.
Thanks,
1) Create subclass of UIView that will represent row in picker. Define property, for example, isChecked, which will show/hide checkmark on this view
2) In the – pickerView:didSelectRow:inComponent: call – viewForRow:forComponent: for previously selected row, set isChecked=NO
3) call – viewForRow:forComponent: for currently selected row and set isChecked=YES;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
MyCustomRowView *prevRowView = [pickerView viewForRow:currentlySelectedRow forComponent:component];
prevRowView.isChecked = NO;
MyCustomRowView *currentRowView = [pickerView viewForRow:row forComponent:component];
currentRowView.isChecked = YES;
//then save currently selected row
currentlySelectedRow = row;
}
4) you also should check currently selected row when you is requested for it:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
....
//Create or reuse view
....
rowView.isChecked = (row == currentlySelectedRow);
}
As you are not providing any code, all we can give you is a general advise;
make sure the viewController showing your UIPickerView instance is inheritating the UIPickerViewDelegate-protocol
set the delegate of your UIPickerView towards this viewController; e.g. pickerView.delegate = self;
implement - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
within the above implementation, remove any checkmark previously added and add a new one to the row selected.

UIPicker noob question - If I click on first item in the picker, the result is null - all other items are fine

I have a UIPicker with 7 items in it. (only one component)... If I click on the first item in the Picker (without ANY SCROLLING of the picker), the text results I get back are null.
However, If I so much as "tug" downward on the list and THEN CLICK item #1 it works fine.
If I click ANY other item, it always returns the right result. Of course, the list has to scroll in order for those items to get selected.
What is happening to cause this? Is there a delegate function I need to call? Is there some way to default the selection to the first item in the list, if they never click anything?
Thanks,
Phil
Here is a snippet from "viewdidload" in my .m
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#">>> Entering %s <<<", __PRETTY_FUNCTION__);
listOfTerms=[[NSArray alloc]initWithObjects:
#"Fall Semester",#"Spring Semester",#"Summer Session",#"Q1",#"Q2",#"Q3",#"Q4",nil];
...
...
Here are the other 4 UIPickerView delegate functions I have defined.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
return [listOfTerms count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [listOfTerms objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
selectedTerm =[[NSString alloc]
initWithFormat:#"%#",
[listOfTerms objectAtIndex:row]];
}
pickerView:didSelectRow:inComponent: is only called when the picker view scrolls. So if you never scroll the view, the method never gets called and therefore selectedTerm is never set. The fix is simple, initialize selectedTerm in viewDidLoad:.
The most simple answer for all situation is to imply didSelectRow in viewDidAppear
(Please note that it is NOT viewDidLoad method)
- (void) viewDidAppear:(BOOL)animated{
[self pickerView:[self yourpickerview] didSelectRow:0 inComponent:0;
}