UIPickerView realtime update on TextField - iphone

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

Related

UIPickerView multi selection on ios7

I need a picker view witch i can select multi values, like the "select input" in HTML :
In ios6, i did it with a custom view created in the UIPickerViewDelegate (pickerView:viewForRow:forComponent:reusingView:) and a UIButton in each row, but since ios7, the custom view don't received the touch event.
Is it possible to do it in ios7 ?
DrDisc has confirmed that it is not possible to handle a touch event directly from a row view since ios7.
But it is possible to :
add a Tap Gesture to the UIPickerView
retrive the selected view
call a method to check / uncheck the row
int row = [self.pickerView selectedRowInComponent:0];
UIView *rowView = [self.pickerView viewForRow:row forComponent:0];
if([rowView isKindOfClass:[YouCustomView class]])
{
[(YouCustomView*)rowView toggleCheck];
[self.pickerView reloadAllComponents];
}
I think it is more natural than a button to check/uncheck, but we lost the ability to select an other row with a tap on it.
As far as I know, this is not possible. One option is to utilize UIPickerView's delegate method:
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
You could store the selection in an array and change the text passed through from the data source to some sort of selection text ("x Selection 1").
Alternatively, you could have a 'select item' button that would add the currently displayed value to the selected array.
When the user clicks a button you can then look through the selected array for those that were selected.
These may not be the best methods, they're just my initial thoughts on it.

How to add an overlay to view the selected values in UIPickerView?

I have created a UIPickerView and I want to add an overlay view to show the user what he has selected. I don't know how to add it. Is there any property for that?
For example when we are selecting a date some overlay view will be there in the middle of UIPickerView, How should I get that ?
like: http://www.inexika.com/blog/Customizing-UIPickerView-UIDatePicker
In the UIPickerView , define
Add
pickerView.showsSelectionIndicator = YES;
and use the following method :-
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
Now in this add a UIView which you can call here, or just allocate it here itself adding what you want to addSubView in the UIView. Hope this much information is sufficient otherwise please tell . Thanks :)

How to get the row number of the selected value of UIPickerView

I have a UIPickerView in my viewController. I can get the value of the row number of the selected value in the picker from the delegate method
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
But in my case, I need to get the row number of the selected component as soon as the view appears. Is there a way to get the row number/trigger the delegate method as soon as the view appears.
You just have to use selectedRowInComponent
selectedRowInComponent:
Returns the index of the selected row in a given component.
- (NSInteger)selectedRowInComponent:(NSInteger)component Parameters
component
A zero-indexed number identifying a component of the picker view.
Return Value
A zero-indexed number identifying the selected row, or -1 if no row is
selected. Availability
Available in iOS 2.0 and later.
See Also
– selectRow:inComponent:animated:
Related Sample Code
iPhoneCoreDataRecipes
Declared In UIPickerView.h
you use this for select in pickerview
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *str= [yardsArray objectAtIndex:row];
}
use this code when view will load
-(void)ViewDidLoad
{
NSString *str= [yardsArray count];
}
in viewDidLoad
travers the array of your objects and match object of which you want to find out the row
and tell me if this is the solution

Multiple UIPickerView with same data in one view. How can I get the content of both picker?

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;

instantiate UIPicker with uitextfield string

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 !