【问题标题】:UIPickerView delegate is called infinite number of timesUIPickerView 委托被无限次调用
【发布时间】:2015-10-24 17:58:28
【问题描述】:

我有一个非常旧的应用程序,其中有 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:@"العربية"];
    

每当我运行这段代码时,我总是看到下面的输出...

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

知道出了什么问题吗?

【问题讨论】:

  • 尝试删除if (screenHeight==568) { [pickerView setFrame: CGRectMake(10, 332, 300, 216)]; } else { [pickerView setFrame: CGRectMake(10, 244, 300, 216)]; }会发生什么?
  • @anhtu : hhhhhhhh...多么好的答案...我不敢相信....它现在可以工作了....
  • 我认为“genderLabelTapped”方法会在您的代码中不断执行。
  • @V.J. :不...检查anhtu评论...使其工作
  • @anhtu:你能把这个作为答案发布吗?我会接受的……

标签: ios objective-c delegates uipickerview


【解决方案1】:

当您 setFrameUIPickerView 时,它将调用 -(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:如果您想在其委托/回调方法中更改对象的任何内容,请谨慎操作。

【讨论】:

  • 你以前发生过这种情况吗? bcz 指出这样的事情并不容易......实际上我正在处理这个项目,其他人在 3 年前工作过...... :(
  • @FahimParkar,不,我以前没有处理过这个问题。我只是猜测。所以我没有先发布答案。
猜你喜欢
  • 1970-01-01
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-29
  • 1970-01-01
相关资源
最近更新 更多