簡體   English   中英

UIPickerView委托被無限次調用

[英]UIPickerView delegate is called infinite number of times

我有一個非常舊的應用程序,其中有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...===العربية

知道出了什么問題嗎?

當您setFrame UIPickerView setFrame時,它將調用-(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:如果要在其委托/回調方法中更改對象的任何內容,請小心操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM