Am I obliged to implement the [UIPickerView] - ios

for my pickerView, i have implemented the DataSource protocol, but for the delegate methods, i don't need any of their, usually i see sample examples on the net implement this method for the delegate :
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
For my case i don't even need it, can i not implement any delegate method or it's obligatory ?? thx in advance :)

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

UIPickerView pickerView:titleForRow:rowforComponent method not called

I have a UIPickerView in my storyboard.
The UIViewController containing it has interface declarations set
#interface ContactDetail : UIViewController <ABPeoplePickerNavigationControllerDelegate, UIPickerViewDataSource, UIPickerViewDelegate,UIAlertViewDelegate, UITextFieldDelegate>
on view Load, pickerview delegation set
pickerView_RelationshipType.dataSource=self;
pickerView_RelationshipType.delegate = self;
pickerView_RelationshipType.showsSelectionIndicator = YES;
all UIPickerView datasource and delegate methods are triggered except those 2
pickerView:titleForRow:rowforComponent
and
pickerView:viewForRow:forComponent:reusingView
those following are called, no problem
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
what's in numberOfComponentsInPickerView ?
If your numberOfRows returning 0, then your delegate method will never be called - the picker won't ask for the title of a row if it doesn't think it has any rows to display.
Make sure your data source array is allocated and initialized. I ran into this problem when everything was hooked up correctly (I set the data source and delegates and was conforming to the protocols), but I forgot to initialize my data source arrays before adding objects to them. My code was hitting numberOfComponentsInPickerView and numberOfRowsInComponent but not titleForRow and I couldn't figure out why. Make sure your data source is not returning nil.
Kind of a stupid edge case, but I figured I'd add my answer in case someone else runs into the same issue.

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

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.

iOS UIPickerView how to get the selected row outside the delegate method

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

Resources