Search code examples
iosobjective-cdelegatesuipickerview

UIPickerView delegate is called infinite number of times


I have a very old app where I have UIPickerView where delegate method is called n number of times & because of which application is crashing.

Below is what I have.

  1. I have already connection UIPickerView to its delegates.

  2. I have label and on tapping on it, I am showing gender in uipickerview.

    langLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGestureRecognizergenderLabel = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(genderLabelTapped)];
    tapGestureRecognizergenderLabel.numberOfTapsRequired = 1;
    [langLabel addGestureRecognizer:tapGestureRecognizergenderLabel];
    [tapGestureRecognizergenderLabel release];
    
    -(void) genderLabelTapped {
        NSLog(@"genderLabelTapped");
        [normalPicker reloadAllComponents];
        normalPicker.hidden = NO;
        pickerImage.hidden = NO;
        continueButton.userInteractionEnabled = NO;
    }
    
  3. I have delegates as below.

    #pragma mark -
    #pragma mark PickerView DataSource
    
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        NSLog(@"numberOfComponentsInPickerView");
        return 1;
    }
    
    -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        CGFloat screenHeight = screenRect.size.height;
    
        if (screenHeight==568) {
            [pickerView setFrame: CGRectMake(10, 332, 300, 216)];
        } else {
            [pickerView setFrame: CGRectMake(10, 244, 300, 216)];
        }
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 204, 44)]; // your frame, so picker gets "colored"
    
        label.textColor = [UIColor blackColor];
        label.font = [UIFont fontWithName:[NSString stringWithFormat:@"%@", localize(@"myFontNameB")] size:14];
        label.textAlignment = NSTextAlignmentCenter;
    
        label.text = [NSString stringWithFormat:@"%@", [arrayGender objectAtIndex:row]];
        NSLog(@"changing font 1111...===%@", [arrayGender objectAtIndex:row]);
        return label;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
        return [arrayGender count];
    }
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        return [arrayGender objectAtIndex:row];
    }
    
  4. Gender array I have is as below.

    arrayGender = [[NSMutableArray alloc] init];
    [arrayGender addObject:@"ENGLISH"];
    [arrayGender addObject:@"العربية"];
    

Whenever I run this code, I always see below output...

    Oct 24 12:36:25 HardTask-iPad AppName[543] <Warning>: changing font 1111...===ENGLISH
    Oct 24 12:36:25 HardTask-iPad AppName[543] <Warning>: changing font 1111...===العربية
    Oct 24 12:36:25 HardTask-iPad AppName[543] <Warning>: changing font 1111...===ENGLISH
    Oct 24 12:36:25 HardTask-iPad AppName[543] <Warning>: changing font 1111...===العربية
    Oct 24 12:36:25 HardTask-iPad AppName[543] <Warning>: changing font 1111...===ENGLISH
    Oct 24 12:36:25 HardTask-iPad AppName[543] <Warning>: changing font 1111...===العربية
    Oct 24 12:36:25 HardTask-iPad AppName[543] <Warning>: changing font 1111...===ENGLISH
    Oct 24 12:36:25 HardTask-iPad AppName[543] <Warning>: changing font 1111...===العربية
    .
    .
    .
    Oct 24 12:36:26 HardTask-iPad AppName[543] <Warning>: changing font 1111...===ENGLISH
    Oct 24 12:36:26 HardTask-iPad AppName[543] <Warning>: changing font 1111...===العربية
    Oct 24 12:36:26 HardTask-iPad AppName[543] <Warning>: changing font 1111...===ENGLISH
    Oct 24 12:36:26 HardTask-iPad AppName[543] <Warning>: changing font 1111...===العربية

Any idea what is going wrong?


Solution

  • When you setFrame of UIPickerView it will call -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view.

    In this case, to stop the loop, remove:

    if (screenHeight==568) {
        [pickerView setFrame: CGRectMake(10, 332, 300, 216)];
    } else {
        [pickerView setFrame: CGRectMake(10, 244, 300, 216)];
    }
    

    P/s: If you want to change anything of an object in its delegate/callback methods, do carefully.