Very simple iPhone question. How to make UIPickerView with two columns? - iphone

I have read other questions about creating a UIPickerView with two or more columns but cannot find the exact solution.
How is this done for the iPhone programmatically? How do you add static data? Thanks!

Make your controller (or whatever is controlling the behavior of the PickerView) support the UIPickerViewDelegate protocol. Then, implement:
- (int) numberOfColumnsInPickerView:(UIPickerView*)picker
to return the number of columns you want, and
- (int) pickerView:(UIPickerView*)picker numberOfRowsInColumn:(int)col
to return the number of rows for each column, and finally:
- (UIPickerTableCell*) pickerView:(UIPickerView*)picker tableCellForRow:(int)row inColumn:(int)col
to setup each cell.
See the reference for UIPickerView and UIPickerViewDelegate.

There is an excellent tutorial on this subject here.

Assuming you have a dictionary or a couple of arrays holding your static data. For simplicity I will go with a very simple array.
You have to modify your view controllers interface definition to tell the program that your view controller can provide data and delegation to a picker view.
#interface NVHomeViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
Than just implementing a couple of methods will make it work however documentation should be checked for other methods that are optional but provides more customization and control.
#interface NVHomeViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
NSArray *options;
- (void)viewDidLoad
{
[super viewDidLoad];
options = #[#"a",#"b",#"c",#"d"];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [options count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [options objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(#"%# selected.",[options objectAtIndex:row]);
}

Related

The PickView is not visible when I run iPhone simulator

I drag a PickerView and a button to the View in my xib file.
But, when I build my code, only my button shows in the simulator. It seems that the PickerView has been hidden because I have got the data from the PickerView.
What is the reason for this problem? How can I fix it?
You must implement at least 2 methods from the UIPickerViewDataSource and 1 from the UIPickerViewDelegate to have the picker view show up. The following 3 methods create a picker view that just displays the word "text" in 3 columns and 12 rows:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return #"Test";
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 12;
}
And do not forget to declare your UIViewController class as delegate of UIPickerViewDataSource and UIPickerViewDelegate:
#interface YourCustomClassViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
In YourCustomClassViewController.h file,
#interface YourCustomClassViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
IBOutlet UIPickerView *pickerView;
}
In YourCustomClassViewController.m file, You can add following methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return #"YourName";
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 5;
}
In your xib file(YourCustomClassViewController.xib), connect Picker to File's Owner. Like following way...
pickerView --->Picker
dataSource --->Picker
delegate ---->Picker
I hope it will be helpful to you surely. If you have any doubt ask to me.

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 create two multiple UIPickerviews in single view controller

I need to create two UIPickers Dynamically with different input values.
I did n't find correct solution in google.
Can any pls post some code.
Thank u in advance.
There are two approaches to this.
a) First, make the view controller the delegate and data source of both the pickers. Then in the view controller's implementation do something like this:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return ( pickerView == picker1 ? 2 : 3 );
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSArray *values = ( pickerView == picker1 ? values1 : values2 );
return [values count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSArray *values = ( pickerView == picker1 ? values1 : values2 );
return [values objectAtIndex: row];
}
Note that you compare the pickerView parameter to an instance variable pointing to one of your picker views and decide "on the fly" which values to return for each of the picker views.
b) Assign different data sources and delegates to each of the picker views (they may be any objects implementing UIPickerViewDelegate and UIPickerViewDataSource protocols, not necessarily the view controller).

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!