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