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).
Related
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;
}
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];
}
I have a core data based app I am working on, that uses an EditingViewController to control a number of different UI elements that get input from the user which describe attributes of an object that is subsequently stored within my app. The EditingViewController.xib file has such elements as a datePicker, textField, numberSlider, and where my problem is coming from a UIPickerView, that are all controlled within one view using .hidden = YES/NO; expressions. My problem is that I need to populate a UIPickerView in two seperate screens, that need to have two different NSMutableArray sources. Inside my viewDidLoad method I setup my first NSMutableArray:
listOfItems = [[NSMutableArray alloc] init];
[listOfItems addObject:#"Greenland"];
[listOfItems addObject:#"Switzerland"];
[listOfItems addObject:#"Norway"];
[listOfItems addObject:#"New Zealand"];
[listOfItems addObject:#"Greece"];
[listOfItems addObject:#"Rome"];
[listOfItems addObject:#"Ireland"];
And after that, I populate my UIPickerView *picker with this code:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker;
{
return 1;
}
- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
label.text= [listOfItems objectAtIndex:row];// The label is simply setup to show where the picker selector is at
}
- (NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component;
{
return 8;//[listOfItems count];
}
- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component;
{
return [listOfItems objectAtIndex:row];
}
And that works fine for the first attribute and array. So then after that, when a different uitableviewcell is selected, the picker is hidden picker.hidden = YES; and I need to have another picker, picker2 show up with a different array of information. But when I try and duplicate the process, by setting up a whole new picker, calling it picker2, and trying to populate it with a different NSMutableArray that I created right next to the first one (again, all because in my EditingViewController it's all part of the same view, just calling different UI elements depending on the view) I can't get picker2 to populate with the new array. I don't know how to set it up so that my two different arrays can be populated. Do I need two picker views? Can it be done with just one? What is the proper syntax in the - (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component; method to make the two arrays viewable seperately? I hope someone can help me out on this, thank you in advance!
You are asking to use the same object as the delegate for two pickers.
In each of the delegate methods, the picker is provided as the first argument for this purpose. You can check which picker is passed in and return the appropriate data for that picker.
For example, if you add a property for the first picker named "pickerOne" and you second mutable array is called "arrayTwo", the delegate methods would look like this:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker;
{
return 1; // assuming both pickers only have 1 component
}
- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
// The label is simply setup to show where the picker selector is at
if (picker == self.pickerOne) {
label.text= [listOfItems objectAtIndex:row];
} else {
label.text= [arrayTwo objectAtIndex:row];
}
}
- (NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component;
{
if (picker == self.pickerOne) {
return [listOfItems count];
} else {
return [arrayTwo count];
}
}
- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component;
{
if (picker == self.pickerOne) {
return [listOfItems objectAtIndex:row];
} else {
return [arrayTwo objectAtIndex:row];
}
}
Also, you can populate your array like this (if you just have a static list of strings, the array does not need to be mutable):
listOfItems = [[NSArray alloc] initWithObjects:#"Baptisms",#"Greenland",#"Switzerland",#"Norway",#"New Zealand",#"Greece",#"Rome",#"Ireland",nil];
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]);
}
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!