Is there Number spinner control for ios? - 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];
}

Related

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.

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 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.

How would I set up a UIPickerView? [closed]

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

Resources