I want to create 2 separate pickers in the same view using the same viewController.
But how do I set separate delegates and datasource for them?
Can't seem to get it working. They show up with the same data. If you have any sample code on this it will be much appreciated.
Thanks.
Note that each method of both the datasource and the delegate protocols contain a UIPickerView * parameter, for instance:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
You need to use it to distinguish between your two instances, as follows:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if([pickerView isEqual: pickerOne]){
// return the appropriate number of components, for instance
return 3;
}
if([pickerView isEqual: pickerTwo]){
// return the appropriate number of components, for instance
return 4;
}
}
The most straight forward way to do this is to use the tag property of the pickerView. I usually define these in the header for readability. You can set the tag in Interface Builder or in code.
#define kPickerOne 0
#define kPickerTwo 1
Then in your implementation file...
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if(pickerView.tag == kPickerOne)
{
// do something with picker one
}
else if(pickerView.tag == kPickerTwo)
{
// the other picker
}
}
Related
I am trying to create a picker like the one in the picture below. The picker I create usually have a smaller border.
How do you create a picker like the picture below.
Implement the
pickerView:widthForComponent
method of the UIpickerViewDelegate Protocol.
like this:
- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
//if you have just one component, simply return a value you want
return 120.0;
}
I am trying to pass my UItextfield value over to a UIPicker that I have loading in a alertview I'm just not sure what functions are available to me to get the values back into the picker?
At a guess I should be doing something in my
- (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
{
if (pv == pickerViewA) {
return 10;
}
else
return 16;
}
but looking at the developers notes their is not much info about the UIPickerViewDataSource other than returning number of rows...
any help would be greatly appreciated.
There are other data source and delegates as-
//for title of a row in a component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
//for width of a componenet
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
//for row height of a component
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
//for number of components
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
for more info look at documentation -
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIPickerViewDelegate_Protocol/Reference/UIPickerViewDelegate.html
and
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPickerView_Class/Reference/UIPickerView.html
You can use either one of the following two delegates to add data content to UIPickerView
– pickerView:titleForRow:forComponent:
– pickerView:viewForRow:forComponent:reusingView:
For the first one u can return any NSString object and you cannot customize anything like font size or color for that.
But if you use the second one you can add the data content as a view to UIPickerView which can be achieved using a UILabel and here you can customize the view as you require.
Hope this might help you
Happy coding !
I have a UIPickerView where the numberOfComponentsInPickerView=4.
But how do I populate each component, as they have different value ranges?
Thanks
by implementing – pickerView:numberOfRowsInComponent: of the pickers datasource.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 4;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0){
return 5;
} else if (component == 1){
return 3;
} else if (component == 2){
return 10;
} else if (component == 3){
return 5;
}
return 0;
}
and the pickerView's delegate
either – pickerView:titleForRow:forComponent: or – pickerView:viewForRow:forComponent:reusingView:
If the components change, you can call [aPickerView reloadComponent:<numberOfComponent>]
or [aPickerView reloadAllComponents] if even the number of components changes.
The dataSource and the viewDelegate documentation.
edit
I just put my very first iPhone program on GitHub, that is a coffee configuration based on a 3 or 4 component picker: You can select amount, kind and one option for every coffee. Except for Latte — there you can choose two different options. You will find it in this repository as "M18Coffee". As I said: my very first program — might be rough.
i am working on a project in which i have to perform following two work:
1.) Fetch value from CoreData and store it in an NSMutableArray.
2.) Take a UIPickerView and fill it with an Array value.
Problem is that size of array is dynamic and i canto fill an array value in UIPickerView. can someone help me.
in order for the UIPickerView to work correctly, you must supply the amount of components (columns) and the number of rows for each component whenever it reloads:
as mentioned, use [myPickerView reloadAllComponents]; to reload the view once the array is populated, but you MUST implement also these things after you declare the containing view controller class as <UIPickerViewDelegate> link the picker to the file owner as a delegate, and then:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;// or the number of vertical "columns" the picker will show...
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (myLoadedArray!=nil) {
return [myLoadedArray count];//this will tell the picker how many rows it has - in this case, the size of your loaded array...
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//you can also write code here to descide what data to return depending on the component ("column")
if (myLoadedArray!=nil) {
return [myLoadedArray objectAtIndex:row];//assuming the array contains strings..
}
return #"";//or nil, depending how protective you are
}
After updating the value (Inserting or modifying existing) in your array.
Call reloadAllComponents on your UIPickerView instance.
- (void)reloadAllComponents
Use as below
[myPickerView reloadAllComponents];
In my project i am filling the picker view array with the value which i am getting from the service.And in the same view i have another label on click of button in the label i want to get the same uipickerview with different value is that possible.....so with one picker view my work will be done no need to call to different picker views...anyone has solution for this isssue
i dont think so .. it will only be possible if you load the data again which means the uipicker(all same instance) with loaded with same data
of course this is possible. Just switch to another model that holds the data that is used in the picker.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
if (myCurrentEditingValue == MyCountryValue) {
return ...
}
else if (myCurrentEditingValue == MyCityValue) {
return ...
}
return nil;
}
- (IBAction)startEditingCountryField {
myCurrentEditingValue = MyCountryValue;
[picker reloadAllComponents];
}
You should get the idea.