UIPickerView for Iphone - 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];
}
}

Related

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"]

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.

how to place previously selected value at UIPickerview bar

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

how to add picker view programmiticaly for list of image names

Im new to iphone development.In my application i want to add image picker for list of image names.here i added picker view and i dont no how to add list of image names programmiticaly in iphone with out using IB.
can any one plz give me code for displaying image names programmiticaly in pickerview.
Thankyou in advance.
#user549767 the answer of dks1725 is correct way to give images to your pickerview.....but in order to add pickerView programatically follow the below link....
UIPickerview in iphone
in viewDidLoad method make an array of your image names like below
-(void)viewDidLoad
{
arr=[[NSArray alloc] initWithObjects:#"image_name1",#"image_name2",#"image_name3",nil];
}
Now in picker view's delegate method write below code
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [arr count];
}
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [arr objectAtIndex:row];
}

Dynamically Add values to UIPickerView at run time

How can you dynamically add values to UIPickerView at runtime.
I'm using the following code to populate a UIPickerView statically. Need to add
values dynamically at run time, for e.g. Three, Four etc.
- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *title = nil;
if(row==0){
title = #"One";
}
if(row==1){
title = #"Two";
}
If you had an array of the titles, you could use something like:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [titles objectAtIndex:row];
}
The titles will be refreshed when you call [pickerView reloadAllComponents] (or reloadComponent: if you have more than one column and only want to refresh one of them).
Hey I had a problem with UIPickerView..... when I call reloadAllComponents it wasn't calling widthForComponent, but it was calling other delegate methods such as titleForRow.... I have no idea why this is, perhaps apple doesn't want the width changing dynamically? anyway it was on another screen so I didn't have the animation/no-animation issue. I found that by resetting the delegate of the picker, I could get widthForComponent to be called again!
i.e.
picker.delegate = view;
where view implements UIPickerViewDelegate.... hope this helps someone out there!