UIPickerView委托被无限次调用

时间:2015-10-24 09:43:47

标签: ios objective-c delegates uipickerview

我有一个非常古老的应用程序,我有UIPickerView,其中委托方法被调用n次&因为哪个应用程序崩溃了。

以下是我所拥有的。

  1. 我已经将UIPickerView连接到其代理人。

  2. 我有标签并点击它,我在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. 我有代表如下。

    #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. 我所拥有的性别数据如下所示。

    arrayGender = [[NSMutableArray alloc] init];
    [arrayGender addObject:@"ENGLISH"];
    [arrayGender addObject:@"العربية"];
    
  5. 每当我运行此代码时,我总会看到以下输出...

        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...===العربية
    

    知道出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

setFrame UIPickerView时,它会调用-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

在这种情况下,要停止循环,请删除:

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

P / s:如果要在其委托/回调方法中更改对象的任何内容,请小心。