harsh chauhan
harsh chauhan

Reputation: 139

unable to pick default value with out scrolling in picker view

I have picker view in my app it works fine but problem if i want to select those value which is in picker view first in.I could not be able to pick without scroll the picker view.Firstly i scroll up or down and after that i can select those value. Not on select with out scrolling. Unable to fetch value with out scrolling in picker view(when user not inter-act with picker view I want to get value which selected by default in picker view).

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

    return [ ArrayForIdealCalc count];

}
#pragma data source
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [ArrayForIdealCalc objectAtIndex:row];
    // return [arrayname objectAtIndex:row];
    //  return [Selected_Centers objectAtIndex:row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    if ([callCalculate isEqualToString:@"WEIGHTMEASURE"])
    {
        IdealWeightstr =[ArrayForIdealCalc objectAtIndex:row];
        _KiloLbl.text=IdealWeightstr;
        [_pickDataForIdealWeight setHidden:YES];
        [self.pickDataForIdealWeight reloadAllComponents];

    }

This is my editable code

Upvotes: 2

Views: 1431

Answers (3)

soul
soul

Reputation: 440

If you want to select a default value. Find the row and use this method of UIPickerView.

– selectRow:inComponent:animated:.

Reference

Upvotes: 2

Milan Gupta
Milan Gupta

Reputation: 1181

You can get the user tap/click to a row of picker view by adding Tap gesture to you picker view. Here is the code for the same.

[self.pickerview selectRow:0 inComponent:0 animated:YES];
UITapGestureRecognizer *tapToSelect = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pickerTapped:)];
tapToSelect.delegate = self;
[self.pickerview addGestureRecognizer:tapToSelect];  

Then add following methods:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
  return true;
}


-(void)pickerTapped:(UIGestureRecognizer *)gestureRecognizer
 {
    if ([self.pickerview selectedRowInComponent:0] == 0)
    {
      _Kilolbl.text = selectedRowValue;
    }
 }  

This is how, you can populate the value of your default selected row in Picker view to the UIControl you wish.If your default selected row is say 10 for example then you should use 10 for the comparison in pickerTapped method.
Do not forget to add UIGestureRecognizerDelegate to you view controller.

Upvotes: 0

Mithun kumar
Mithun kumar

Reputation: 662

you can use

selectRow:0 inComponent:0 animated:NO

Or you can call the picker delegate method with

pickerView:myPickerView didSelectRow:0 inComponent:0

Upvotes: 3

Related Questions