Populate UIPicker with custom object field - ios

I have an object with an NSString property. I'd like to populate a UIPickerView with a mutable array of these objects, and use the string property as the title for each row. Any ideas on how to properly do this?
I tried to use the following code below, but the rows are left blank.
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return ((Vehicle*)[vehicleArray objectAtIndex:row]).vehicleType;
}

Related

Is there Number spinner control for ios?

I'm looking for a spinner control something similar to this for ios:
http://github.hubspot.com/odometer/docs/welcome/
can't find anything so far, please share if you know any.
there is one third party library , by which you can do this https://github.com/Rizh/RCounter
https://github.com/naked-apps/NACounter
https://github.com/jonathantribouharet/JTNumberScrollAnimatedView
The nearest thing in iOS, similar to this is UIPickerView. You can create a UIPickerView in the Interface Builder or through code.
Next, make your class conform to UIPickerViewDataSource and UIPickerViewDelegate.
In your viewDidLoad: method, assign the delegate and dataSource of the UIPickerView to the respective class.
And finally add the following methods to your class.
// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 5;
}
// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 10;
}
// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:#"%ul",row];
}

Add User data to UIPickerView

I have an app where the user scans a QR code into an NSString, which I then need to put into a UIPickerView.
What would I use to set the text of the UIPickerView row as the NSString from the QR code?
Firstly, you need to have an array of strings and implement the UIPickerViewDataSource and UIPickerViewDelegate in your .h file. After this, you will have to make use of the delegate methods of UIPickerView like as shown :
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//return number.
}
This will return the number of sections that you want in your pickerView.
(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return array.count;
}
The above method will return the number of rows in particular component. If you have an array, then probably it will be equal to the number of elements in your array. And finally this last one.
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *str;
if(pickerView == yourPickerName)
{
str = [array objectAtIndex:row];
}
return str;
}
This method will return the text value for each row in your picker. And don't forget to connect your pickerView's datasource and delegate in your Storyboard.
Hope this helps.

How to read UIPicker selected text

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];

How do I make a picker view populate from an array of NSStrings? How would I then access the value of the item picked?

Okay, I need a picker so that a user can select from a list of predefined options. Can someone give me the easy, simplified version of how to populate a picker view from an array of NSStrings? Then, how do I read that value from the picker? I'm noticing that things like nameOfPicker.value and nameOfPicker.text do not work here.
Thank you!
I have already read the following documents and I don't really understand what they are getting at.
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UIPickerView.html
https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html#//apple_ref/doc/uid/TP40010810-CH11
It is quite similar to how you populate data in a UITableView, by setting datasource and delegate.
1. First step is to set the delegate of the picker view. In .m file you set your datasource and delegate
#interface YourViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
{
}
#property (strong, nonatomic) UIPickerView *yourPickerView;
2. Assign the datasource and delegate
self.yourPickerView.delegate = self;
self.yourPickerView.datasource = self;
3. Implement datasource and delegate
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.yourArrayofStrings.count;
}
//The title that should be shown in picker view
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *yourTitle = [self.yourArrayofStrings objectAtIndex:row]
return yourTitle;
}
//This is called when Picker view value is selected
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog #(#"Selected row = %#",[self.yourArrayofStrings objectAtIndex:row]);
}

Append 2 webservices data in one uiPickerView component

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.

Resources