I know the UIPickerView has this method:
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
But inside this method, I want to get the selected row of other components. How on Earth can I do that?
You can get selected row number anywhere by using selectedRowInComponent method of UIPickerView.
Such like
NSString *YourselectedTitle = [self.yourArrayName objectAtIndex:[self.yourPickerName selectedRowInComponent:0]];
NSLog(#"%#", YourselectedTitle);
Old question but if someone like me is working with Swift 2.0 now the answer is
let row = self.myPicker.selectedRowInComponent(0)
let value = myArray[row]
I wrote 0 to component value because my picker has only one component, but it could have more than one such as datepicker with Date and Time
You can even use this method outside the function that you were talking about if you keep an outlet of your pickerview
Related
I want to use two picker views in same view where first picker view is of category and when a category is selected, its corresponding values are added to second picker view. How can I make it possible? Values are taken from JSON.
This is fairly simple. You must be populating your second picker view from an array.
Make your view controller the delegate and data source for both the picker views.
Then when the 'func pickerView(UIPickerView, didSelectRow: Int, inComponent: Int)' delegate method is called when you select an item in the first picker view, update the data array for the second picker view and call reloadAllComponents on it
Here is some code
func pickerView(UIPickerView, didSelectRow: Int, inComponent: Int)
{
if (pickerView == self.firstPicker)
{
//calculate your data array for the second picker here
self.secondPickerView.reloadAllComponents()
}
}
You can get value selected in first picker view like this
NSInteger row;
NSArray *firstPickerViewDataArray;
UIPickerView *firstPickerView;
NSString *selectedValue;
row = [firstPickerView selectedRowInComponent:0];
selectedValue = [firstPickerViewDataArray objectAtIndex:row];
Get the data from server or wherever you want and store that into another array like
NSArray * secondPickerViewDataArray;
After that you can use below method to show values
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (pickerView == secondPickerView){
// Do whatever you want based on selected value in first PickerView
return secondPickerViewDataArray[row];
}else{
//stuff for first pickerView
}
}
I have a View Controller with two distinct UIPickerViews. The first is a contains seven names. The second is a list of numbers(strings) from 1 to 100. I want the default to be the last number in the list.
I have set the first UIPickerView's default value using the following method which I call in viewDidLoad.
[self.namePicker selectRow:2 inComponent:0 animated:YES];
This works just fine and my namePicker shows the third element when I run the code.
However, the following method call does not work for my second UIPickerView(also being called in viewDidLoad).
[self.numberPicker selectRow:99 inComponent:0 animated:YES];
There are no errors when I run, the numberPicker just appears with a default value of 1 (the first element) every time.
The issue could be that your UIPickerView dataSource and delegate methods are not differentiating between the two UIPickerViews. One way to handle that would be to give them separate tags in InterfaceBuilder and then use if and/or switch statements in your delegate and datasource methods:
#define kNamesPicker 0 // tag assigned in IB
#define kNumberPicker 1 // tag assigned in IB
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (pickerView.tag == kNamesPicker) {
}
else if (pickerView.tag == kNumberPicker) {
}
Once your delegate and datasource methods are sorted assigning values to the pickers should work fine.
Hi i have 3 picker views from one view controller.imagine view A.when i selected data from view A those data need be go to display other view controller of picker views.I don't know how to do that and i tried lot.please let me know someone know this answer. Thank you.
gihanghost,
UIPickerView has number of delegates to inform its status.
One that you can make use of would be,
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//you can access the selected object using the row value
//you must have provided the array as a data source to your pickerView
YourObject *obj = [YourPickerViewArray objectAtIndex:row]
}
This delegates gets called when user scrolls through pickerView options and each row comes to focus.
If you want to access the currently selected row in pickerView later in your code you can use the code below
NSInteger row = [self.yourPickerViewInstance selectedRowInComponent:0];
YourObject *obj = [YourPickerViewArray objectAtIndex:row]
Now you have the selected object with you. Pass it to whichever view controller you want :)
Happy coding :)
I have created a UIPicker, which has about eight or nine possible strings. Depending on what view loads the UIPicker will depend on which strings are shown.
Due to this in my pickerView:didSelectRow:inComponent so I was wondering if there is a way when the user selects a row if I can get the textvalue that is in that.
This way in the didselectrow method I can perform different actions depending on the string value that was selected.
Current this is what I have:
#pragma mark - Picker Delegates
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// get rowString somehow?
// ReloadView
if ([rowString isEqualToString:#"one Of the Values"]) {
}
// more if statements here.
}
Just read the value from the data source for that row then compare:
NSString *rowString = (NSString *)[self.pickerArray objectAtIndex:row];
if ([rowString isEqualToString:#"one Of the Values"]) {
}
#Meda gave you the right answer. You should not store data in a view object like a picker. Remember, in order for a UIPickerView to work, you have to implement the method pickerView:titleForRow:forComponent:' (or the methodpickerView:viewForRow:forComponent:reusingView:' if you build your cell views yourself). The `pickerView:titleForRow:forComponent:' method provides the text for each row in a component of your picker view, so it has to have the data available to it.
Therefore, the delegate of the UIPickerView needs to have that data available to it. Meda's post assumed you saved it in an array pickerArray.
[pickerView selectedRowInComponent:0];
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am setting up a UIPickerView to have choices like choice a, choice b, choice c and so on. I have tried to interpret the sample code from Apple but that seems very difficult to understand for a beginner like me. I also would like the choices from the picker view to take me to another page if that is possible.
It's obvious for every beginner that it is some what tedious to understand these things the first time.
Anyways, do you know how to use UITableViews? Do you know how to use UITableViewDelegate and UITableViewDataSource? If your answer is yes, then just imagine UIPickerViews are like UITableViews (but remember they are not UITableViewControllers).
Let's say, I've a UIPickerView:
UIPickerView *objPickerView = [UIPickerView new]; // You need to set frame or other properties and add to your view...you can either use XIB code...
1) First you need to assign the delegate and dataSource to the UIPickerView either via IB or code. It depends on your implementation (So this step looks very similar to a UITableView, doesn't it?)
Like this:
objPickerView.delegate = self; // Also, can be done from IB, if you're using
objPickerView.dataSource = self;// Also, can be done from IB, if you're using
2) Next, you need to define number of sections, like this:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1; // Or return whatever as you intend
}
2) Then you need to define the number of rows you need:
- (NSInteger)pickerView:(UIPickerView *)thePickerView
numberOfRowsInComponent:(NSInteger)component {
return 3;//Or, return as suitable for you...normally we use array for dynamic
}
3) Then, define title for row (And if you have multiple section, then title for each section):
- (NSString *)pickerView:(UIPickerView *)thePickerView
titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [NSString stringWithFormat:#"Choice-%d",row];//Or, your suitable title; like Choice-a, etc.
}
4) Next, you need to get the event when someone clicks on an element (As you want to navigate to other controller/screen):
- (void)pickerView:(UIPickerView *)thePickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
//Here, like the table view you can get the each section of each row if you've multiple sections
NSLog(#"Selected Color: %#. Index of selected color: %i",
[arrayColors objectAtIndex:row], row);
//Now, if you want to navigate then;
// Say, OtherViewController is the controller, where you want to navigate:
OtherViewController *objOtherViewController = [OtherViewController new];
[self.navigationController pushViewController:objOtherViewController animated:YES];
}
That's all the implementation you need.
I'll briefly explain how to achieve that programmatically
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView * picker = [UIPickerView new];
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
[self.view addSubview:picker];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString * title = nil;
switch(row) {
case 0:
title = #"a";
break;
case 1:
title = #"b";
break;
case 2:
title = #"c";
break;
}
return title;
}
Basically in the viewDidLoad we are creating and adding a UIPickerView to the view, telling it that our controller serves both as delegate and dataSource (You can do this in Interface Builder too)
Then we are implementing two data source methods in order to tell how many components and how many rows per components the pickerView has, respectively numberOfComponentsinPickerView: and pickerView:numberOfRowsInComponent:
Finally we are implementing the delegate method pickerView:titleForRow:forComponent: that returns the content of every line.
If you want to customize the behavior when a row has been selected you can implement the delegate method pickerView:didSelectRow:inComponent: which - as the name suggests - is called every time a row is selected.
A UIPickerView use a parten similar to the one use for a UITableView. DataSource and Delegate protocol.
The DataSource methods are used to tell the picker how many 'columns' and how many 'rows' there are in your picker.
While the Delegate methods are for the rest, height of rows, width of columns, views or title to display and a call back when the picker got moved.
UIPickerView