2

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

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
  • Can you post your code? Thanks. – Nader Dabit Dec 08 '15 at 12:19
  • @harsh put your some code here. – Bhavesh Nayi Dec 08 '15 at 12:21
  • -(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]; } } – harsh chauhan Dec 08 '15 at 12:21
  • I could not select first value without scrolling. – harsh chauhan Dec 08 '15 at 12:22
  • @harshchauhan put your code in que. with edit – Bhavin Ramani Dec 08 '15 at 12:23
  • if i send all delegates and data source its size too long – harsh chauhan Dec 08 '15 at 12:24
  • 1
    @harshchauhan: You can choose the first value as the default value. Now, when you scroll, change the value. If its not scrolled, then you can use the default i.e, the first value of the picket. May be that will solve your problem. – neha_sinha19 Dec 08 '15 at 12:26
  • i want choose those value who comes in very first time .And without scrolling i can select.Like we do in table view – harsh chauhan Dec 08 '15 at 12:27
  • @neha:if you coulnt not understood my problem .send me your mailed i ll send my file to you – harsh chauhan Dec 08 '15 at 12:29
  • I put ma code..could any one see...? – harsh chauhan Dec 08 '15 at 12:45
  • 1
    @harshchauhan: I know what your problem is. pickerView:didSelectRow:inComponent: never gets called until you scroll the picker view. Therefore, the first value is not set. The solution to this is the one that I mentioned above. Just select the first one as the default value and change it if the picker is scrolled. This will definitely solve your problem. – neha_sinha19 Dec 08 '15 at 12:46
  • @ Neha: Thanks.It means no any other solution... – harsh chauhan Dec 09 '15 at 05:24

3 Answers3

3

you can use

selectRow:0 inComponent:0 animated:NO

Or you can call the picker delegate method with

pickerView:myPickerView didSelectRow:0 inComponent:0
Mithun kumar
  • 662
  • 3
  • 7
2

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

– selectRow:inComponent:animated:.

Reference

Community
  • 1
  • 1
soul
  • 420
  • 1
  • 3
  • 11
0

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.

Milan Gupta
  • 1,181
  • 8
  • 21