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];
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 :)
Here is my Problem, I am sorry if it is a very frequent question.
I had a web-service from which I was getting projects and Holidays from different methods. Now I just want this Holidays and projects to be loaded into the uipickerview in a single component.
For example I had my projects with names proj1, proj2,proj3 which is coming from getProjects method from web-service.
Similarly I had holidays with names sickleave, casual leave which is coming from getHolidays method from web-service.
So I want in such a way that sickleave,casual leave,proj1,proj2 to be displayed in a single component in a uipickerview
I guess somehow some answers were stating to use NSMutableArray, But I dont know how to use that...
I think you should follow these steps:
Declare and initialize an NSMutableArray.
Use addObject method on your NSMutableArray to add objects in your array from both WebServices. You will be required to loop through till the WebServices are returning values.
Outside of your loop use: [picker reloadAllComponents]
Use picker view delegate methods as:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [yourArray count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1 ;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return [yourArray objectAtIndex:row];
}
Also don't forget to connect your picker view delegate and datasource.
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