how is the code for picker view selected item? - iphone

how is the code for picker view if i selected the any item from the Pickerview it as to show the related alert message.
exe: In picker view consists "RED",YELLOW","BLOCK","GREEN" from the picker view if i selected the RED Alert message "THIS IS RED".
so help me to solve the problem.

You can use the picker delegate method.
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
mString.text= [arrayNo objectAtIndex:row]; //mString is a NSString defined and the arrayNo is the NSArray that contain all your color data.
}
Now you have the selected value in your mString object then use that object in the alert message.
You can refer this discussion

Set a delegate and use - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
in- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
Read the Apple Doc about that:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPickerViewDelegate_Protocol/Reference/UIPickerViewDelegate.html#//apple_ref/occ/intf/UIPickerViewDelegate

Related

error while selecting row in a picker view iphone

i want to show picker view with years , i am storing year array in my userDC entitly class but i am getting an error on log message
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
userdc = [years objectAtIndex:row];
NSLog(#"%d",userdc.user_year);
}
my error is like this
-[__NSCFString user_year]: unrecognized selector sent to instance 0xaaa7220
If your user_year is string then
NSLog(#"%#",userdc.user_year);
And if it is integer then
NSLog(#"%#",[NSString stringWithFormat:#"%d",userdc.user_year]);
ANd if your userdc is dictionary then use
NSLog(#"%#",[NSString stringWithFormat:#"%d",[userdc objectForKey:#"user_year"]);
yeppii .. i solved my own problem :):) by doing this, thanx all who helped me, cheers
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
userdc=[[userDC alloc]init];
userdc.user_year = [years objectAtIndex:row];
NSLog(#"%#",userdc.user_year);
}

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

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

How to call the picker view content in a table view?

how to call the picker view content in table view..is there any method to call the picker view content?pls help me...
I think you would have used this delegate to put your content into the picker.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return yourPickerContentArray;
}
I think that yourPickerContentArray will have the content of the picker. why dont you use that array where you want.