PickerView delegate method...
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (didSelectedRow ==1)
{
strChkDistanceValue = [arrDistance objectAtIndex:row];
}
else
{
strChkDateRangeValue = [arrDateRange objectAtIndex:row];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if(didSelectedRow ==1)
{
return [arrDistance count];
}
else
{
return [arrDateRange count];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if(didSelectedRow ==1)
{
strChkarray =#"arrDistance";
return [arrDistance objectAtIndex:row];
}
else
{
strChkarray =#"arrDateRange";
return [arrDateRange objectAtIndex:row];
}
}
above code I am use...
I am using the pickerview in tableview... there are two picker views...
when i am use the first picker view the first picker view array shows in picker when i am select 3rd row in picker view after i select the 2nd picker view then show the second array list but second array list show start with 3rd row...same to 1st picker...
but i want to show at first row of array in pickerviw..
please help me...
Thanks & regards
Rahul Virja
You should use below method of UIPickerView to select a particular row.
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
This is my code for two pickerviews in one view controller. However its not working for me.
#pragma mark UIPickerViewDelegate methods
//PickerViewController.m
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
switch ([thePickerView tag]) {
case 1: //purpose picker
return [m_arrPurpose count];
case 2: //second picker
return [m_arrSweep count];
default:
return 0;
break;
}
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
switch ([thePickerView tag]) {
case 1: //purpose picker
{
//cost.text = #"Test";
Purpose *prp = [m_arrPurpose objectAtIndex:row];
return [prp m_purposeName];
}
case 2: //second picker
{
OpenActivity *opn = [m_arrSweep objectAtIndex:row];
return [opn m_ahhaName];
}
default:
return #"";
break;
}
}
can any1 help me with this please..
thanks
It sounds like either your tags aren't set correctly or you havent connected the datasource and delegate methods for both pickers.
Add some NSLog statements in the numberOfRowsInComponent and titleForRow: methods.
Include the picker view object and the picker view's tag in the log, e.g.
NSLog(#"Rows in component for %#, tag %d",thePickerView,[thePickerView tag]);
And a different text in the titleForRow.
You should see two different objects - if not, your delegate and datasource are not connected. You should see tags 1 and 2 - if not, your tags are not set properly.
With this problem there was nothing wrong with my code It's just that I forgot to set tag's value to 1 and 2 in XIB.
I have four NSDictionaries that I would like to use to populate a pickerview depending on a segemented control.
With the code I have, the first segmented control/pickerview works fine but when I switch to the second segment the picker view only loads part of the second dictionary, that is it loads the same number of rows as it counted in the first dictionary. When I change the segmented control to the third or fourth segment it simply crashes with a sigabrt error indicating that it cannot index item43 when only 27 exist. This I suspect stems from a UItextfield population based on the upickerview row and object. I think the problem is with the way I have the data source and delegate set up.
#pragma mark -
#pragma mark UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if ([wine selectedSegmentIndex] == 0)
{
return [robskeys objectAtIndex:row];
}
if ([wine selectedSegmentIndex] == 1)
{
return [esabskeys objectAtIndex:row];
}
if ([wine selectedSegmentIndex] == 2)
{
return [lebskeys objectAtIndex:row];
}
else if ([wine selectedSegmentIndex] == 3)
{
return [sbskeys objectAtIndex:row];
}
return #"Unknown title";
}
#pragma mark -
#pragma mark UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if ([wine selectedSegmentIndex] == 0)
{
return robskeys.count;
}
if ([wine selectedSegmentIndex] == 1)
{
return esabskeys.count;
}
if ([wine selectedSegmentIndex] == 2)
{
return lebskeys.count;
}
else if ([wine selectedSegmentIndex] == 3)
{
return sbskeys.count;
}
return 1;
}
#pragma mark -
Any help would be much appreciated
Thank you
UPDATE
Using the following when the segmentedcontrol is changed works. Does anyone see any problems in alloc, init, release the same picker view in viewDidLoad and in this IBAction. As you may have guessed the UItextfield winespec calls the coPicker not a keypad.
-(IBAction)ValueChanged:(id)sender {
[winespec resignFirstResponder];
UIPickerView *coPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
coPicker.tag = kCountryPickerTag;
coPicker.delegate = self;
coPicker.dataSource = self;
[coPicker setShowsSelectionIndicator:YES];
winespec.inputView = countryPicker;
[winespec becomeFirstResponder];
[coPicker release];
}
Are you reloading the UIPickerView when you switch the data source? (The reloadAllComponents method should do the trick.)
Irrespective, the problem is that an "out of index" row is being selected, so you'll probably also need to use the UIPickerView's selectedRowInComponent method to select a "safe" row (the first for example) when you switch the data source.
UPDATE
In essence, when you (effectively) change the current data source via the segmented control you need to set the selectedRowInComponent to zero and call reloadAllComponents on your UIPickerView. That should sort it all out. As such you could put this within the action you use to respond to the segmented control changes.
It may be a simple question but how do I add multiple components to a UIPickerView? I use NSMutableArray to populate one component but I dont know how to populate the others. I also need to change the value of a label when a row is selected.
Thanks in advance
Kieran
I take it that you are a beginner. Here's how to implement the methods nacho has rightly pointed out:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
// return the number of components required
return 2;
}
You can use other NSMutableArray's to populate the components. Assuming that you have 2 components, each using different NSMutableArray i.e. array1 and array2:
// Return row count for each of the components
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return [array1 count];
}
else {
return [array2 count];
}
}
// Populate the rows of the Picker
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// Component 0 should load the array1 values, Component 1 will have the array2 values
if (component == 0) {
return [array1 objectAtIndex:row];
}
else if (component == 1) {
return [array2 objectAtIndex:row];
}
return nil;
}
Use this code to change the text of a label, say selectedValue when a value is selected in the PickerView:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
selectedValue.text = [NSString stringWithFormat: #"%#, %#", [array1 objectAtIndex:[myPicker selectedRowInComponent:0]],[array2 objectAtIndex:[myPicker selectedRowInComponent:1]]];
}
... and you are good to go :)
You might also want to see http://joshhighland.com/blog/2009/09/17/uipickerview-spinning-multiple-components/ for valuable tips on pickerView programming.
You need to set the datasource and use
– numberOfComponentsInPickerView:
– pickerView:numberOfRowsInComponent:
set its delegate and use:
– pickerView:didSelectRow:inComponent:
to change your label when a certain row in a certain component is selected. Very likely to UITableViewDelegate and Datasource
and also taking a glance to documentation helps :)
Instead of component I found using tag more proper.
Then in the code:
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
//NSLog(#"Component_1: %d",pickerView.tag);
if (pickerView.tag == 1)
return [self.arrOpponentTeams count];
else if (pickerView.tag == 2)
return [self.arrMyTeams count];
else
return [self.arrPlayers count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//NSLog(#"Component_2: %d",pickerView.tag);
if( pickerView.tag == 1 )
return [self.arrOpponentTeams objectAtIndex:row];
else if (pickerView.tag == 2)
return [self.arrMyTeams objectAtIndex:row];
else
return [self.arrPlayers objectAtIndex:row];
}
I do not know if it's a little late But I solved this another way Array are a bit rudimentary But it works wonders You can even put different color to each array
- (void) viewDidLoad {
horPickerArray = #[#"00",#"01",#"02",#"03",#"04",#"05".to..24];
minPickerArray = #[#"00",#"01",#"02",#"03",#"04",#"05".to..59];
secPickerArray = #[#"00",#"01",#"02",#"03",#"04",#"05".to..59];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return _HorPickerArray.count ;
}
else if (component == 1) {
return _MinPickerArray.count ;
}
else if (component == 2) {
return _SecPickerArray.count ;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
return [_HorPickerArray objectAtIndex:row];
}
else if (component == 1) {
return [_MinPickerArray objectAtIndex:row];
}
else if (component == 2) {
return [_SecPickerArray objectAtIndex:row];
}
return nil;
}
And Print in TextField
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
_OficialTime.text =[NSString stringWithFormat: #"%#:%#:%#", [_HorPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:0]],[_MinPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:1]], [_SecPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:2]]];
}
Here is the swift3 code.Here I am assigning values of multiple picker view to a UITextField.
var pickOption = [["1", "2", "3","4","5","6","7","8","9"], ["0","1","2", "3","4","5","6"],["0","1","2", "3","4","5","6"]]
#IBOutlet weak var travellersTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let pickerView = UIPickerView()
pickerView.delegate = self
travellersTextField.inputView = pickerView
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return pickOption.count
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickOption[component].count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickOption[component][row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let adult = pickOption[0][pickerView.selectedRow(inComponent: 0)]
let child = pickOption[1][pickerView.selectedRow(inComponent: 1)]
let infant = pickOption[2][pickerView.selectedRow(inComponent: 2)]
travellersTextField.text = adult + " adults," + child + " children," + infant + " infants"
travellersTextField.becomeFirstResponder()
}
I am working on a core data using app and one of the views is EditingViewController, which acts as the controller for a number of ui elements which describe the attributes of objects. Inside the EditingViewController, all of my ui elements are being called and hidden with the .hidden = YES/NO; operation. One of my ui elements is a uipickerview. Currently, there is one array for each of two different views. Meaning that if they clicked on the first name field, array1 would load in picker1, and they would pick from it, and then if they clicked on last name, array2 would load in picker2, and life is good. Here is my code I am using to make this work up until now:
- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (picker1 == self.picker) {
return [array1 objectAtIndex:row];
} else {
return [array2 objectAtIndex:row];
}
}
But! array1 is so big that I would like to add a UISegmentedControl to picker1 so that it can sort array1's info a little better. I know that UISegmentedControl is just a pretty set of buttons, requiring IBActions to be linked and stuff, but how could I implement it so that in one of my views, the first one, just picker1 and array1 are managed by the UISegmentedControl? Is it possible to make it so that it is just a smooth sorting process? I figure I will break down array1 into other arrays based on the conditions I set in my UISegmentedControl, how do I link those so they look and work good? Any help on this topic as always is greatly appreciated! Thanks
Use the selectedSegmentIndex property.
For example:
enum {
SEGMENT1,
SEGMENT2,
SEGMENT3
};
- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (picker1 == self.picker) {
if (segmentedControl.selectedSegmentIndex == SEGMENT1) {
return [array1sub1 objectAtIndex:row];
} else if (segmentedControl.selectedSegmentIndex == SEGMENT2) {
return [array1sub2 objectAtIndex:row];
} else if (segmentedControl.selectedSegmentIndex == SEGMENT3) {
return [array1sub3 objectAtIndex:row];
}
} else {
return [array2 objectAtIndex:row];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (picker1 == self.picker) {
if (segmentedControl.selectedSegmentIndex == SEGMENT1) {
return [array1sub1 count];
} else if (segmentedControl.selectedSegmentIndex == SEGMENT2) {
return [array1sub2 count];
} else if (segmentedControl.selectedSegmentIndex == SEGMENT3) {
return [array1sub3 count];
}
} else {
return [array2 count];
}
}
Also, you will want to reload the picker when the segmented control changes, so you should link the segmented control's value-changed event with a method like this:
- (IBAction)handleValueChanged
{
[self.picker reloadAllComponents];
}