I am trying to pass my UItextfield value over to a UIPicker that I have loading in a alertview I'm just not sure what functions are available to me to get the values back into the picker?
At a guess I should be doing something in my
- (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
{
if (pv == pickerViewA) {
return 10;
}
else
return 16;
}
but looking at the developers notes their is not much info about the UIPickerViewDataSource other than returning number of rows...
any help would be greatly appreciated.
There are other data source and delegates as-
//for title of a row in a component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
//for width of a componenet
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
//for row height of a component
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
//for number of components
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
for more info look at documentation -
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIPickerViewDelegate_Protocol/Reference/UIPickerViewDelegate.html
and
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPickerView_Class/Reference/UIPickerView.html
You can use either one of the following two delegates to add data content to UIPickerView
– pickerView:titleForRow:forComponent:
– pickerView:viewForRow:forComponent:reusingView:
For the first one u can return any NSString object and you cannot customize anything like font size or color for that.
But if you use the second one you can add the data content as a view to UIPickerView which can be achieved using a UILabel and here you can customize the view as you require.
Hope this might help you
Happy coding !
Related
I have 2 UIPickerviews with the same data.
At the moment sb is clicking the save-Button I want to get the data from both PickerViews.
I already set a tag to both pickers and I also implemented the function:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [symbolList objectAtIndex:row];
}
But I have no idea how to get on both datas at the same time.
For each picker view, use selectedRowInComponent: to find out the current selection. Use this to obtain the value from each picker view's data source, e.g:
NSString *pickerViewOneSelection = [symbolList objectAtIndex:[pickerViewOne selectedRowInComponent:0]];
NSString *pickerViewTwoSelection = [symbolList objectAtIndex:[pickerViewTwo selectedRowInComponent:0]];
I'm assuming that pickerViewOne and pickerViewTwo are pointers to your two picker views and you've already worked that part out. I have also assumed that your picker has only one component.
Set tag of pickerview.
first create IBOutlet of both picker view.
and set tag of picker view.
pickerview.tag == 10;
I am trying to center and change the size of the text inside my UIPicker but am not sure how... I think it might happen in one of these methods, most likely the first...
- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [[NSString stringWithFormat:#"%x",row ] uppercaseString]; //Sets picker options as an Uppercase Hex value
}
#pragma mark UIPickerViewDataSource methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pv
{
return 5; //5 columns in the picker
}
- (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
{
return 16;
}
Dose anyone one know how this can be done? I have looked around but the documentation is fairly thin...
Johns,
As in Table Views, you can't change the font attributes of the default title that you usually set in the titleForRow delegate method, instead you have to use the following method:
- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component
Here you can create an UIView with a custom UILabel and set the Label's font size, color and other attributes.
This method is described in Apple's UIPickerView Class Reference
Hope this help you!
Now with iOS 6 you can use NSAttributedStrings to customize the title text (though I don't think this lets you easily center the text).
A new UIPickerViewDelegate method lets you return an attributed string instead of a plain vanilla string:
(NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
The NSAttributedString UIKit additions page has a list of all the character attributes you can control:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/Reference/Reference.html#//apple_ref/doc/uid/TP40011688
As soon as I scroll the PickerView (or) UIDatePicker, I need the value to get displayed on a TextField :
While Scrolling. Even when the picker is moving the text field should update itself. Is it possible ?
On the event that the Picker stops moving and comes to rest. Is there an event for the PickerView to come to rest so that I could update the textField (as soon as the pickerView comes to rest).
// I do not wish to use the TextFieldDidEndEditing
// I want the textField to update itself when the picker view comes to rest.
It would be helpful if anyone could solve 1) and 2)
(1) might not be possible. For (2), you can use the delegate method pickerView:didSelectRow:inComponent. Let me know if you need any help with this.
For UIPickerView
(1) Implement pickerView:titleForRow:forComponent,
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// Every time the picker view is rotated, this method will be called to fetch the title.
NSInteger selectedRow = [pickerView selectedRowInComponent:0];
// Get selected row like above and process data and update text field.
[..] // Process the title
return the title.
}
(2) This is more obvious.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// You have the selected row, update the text field.
}
For the first question, this isn't possible with the default control however there is nothing to stop you writing your own and simulate the effect using touchesMoved etc...
i am working on a project in which i have to perform following two work:
1.) Fetch value from CoreData and store it in an NSMutableArray.
2.) Take a UIPickerView and fill it with an Array value.
Problem is that size of array is dynamic and i canto fill an array value in UIPickerView. can someone help me.
in order for the UIPickerView to work correctly, you must supply the amount of components (columns) and the number of rows for each component whenever it reloads:
as mentioned, use [myPickerView reloadAllComponents]; to reload the view once the array is populated, but you MUST implement also these things after you declare the containing view controller class as <UIPickerViewDelegate> link the picker to the file owner as a delegate, and then:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;// or the number of vertical "columns" the picker will show...
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (myLoadedArray!=nil) {
return [myLoadedArray count];//this will tell the picker how many rows it has - in this case, the size of your loaded array...
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//you can also write code here to descide what data to return depending on the component ("column")
if (myLoadedArray!=nil) {
return [myLoadedArray objectAtIndex:row];//assuming the array contains strings..
}
return #"";//or nil, depending how protective you are
}
After updating the value (Inserting or modifying existing) in your array.
Call reloadAllComponents on your UIPickerView instance.
- (void)reloadAllComponents
Use as below
[myPickerView reloadAllComponents];
I have a simple UI picker view in an iOS app (iPhone) and I'm looking to pre-populate it with a range of numbers on launch. What would be the most pragmatic/quickest/optimized way to populate it? I'm new to iOS development, so I'm just starting to test the waters. The documentation is pretty decent but I'd like to get some insight from seasoned developers on the most effective way to accomplish what I'm doing?
tl;dr
I want to populate a UI picker view with the the number range 45-550 upon the start of my application, what's the best way to do so?
Despite the possibility that I may not qualify as a seasoned developer, I would do it this way. In the class that will be the data source and delegate of the picker view:
#define PICKER_MIN 45
#define PICKER_MAX 550
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return (PICKER_MAX-PICKER_MIN+1);
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:#"%d", (row+PICKER_MIN)];
}