簡體   English   中英

UIView似乎不記得它的標簽

[英]UIView doesn't seem to be remembering its tag

我正在使用CPPicker在應用程序中實現水平選擇器,並且正在使用標簽來確定我正在談論的兩個選擇器中的哪一個。

     // Picker creation
     CPPickerView *pickerView = [[CPPickerView alloc] initWithFrame:CGRectMake(100, 5, 150, 40)];
     pickerView.dataSource = self;
     pickerView.delegate = self;
     pickerView.peekInset = UIEdgeInsetsMake(0, 40, 0, 40);
     [pickerView reloadData];
     pickerView.showGlass = YES;
     [cell addSubview:pickerView];

    if (indexPath.section == 0) {
        pickerView.tag = 0;
    }
    else if (indexPath.section == 1) {
        pickerView.tag = 1;
    }

    return cell;

然后,我檢查標簽以指定pickerView的標題。 但是它只會讀取0,因此兩個選擇器的值相同。

- (NSString *)pickerView:(CPPickerView *)pickerView titleForItem:(NSInteger)item {
    NSString *title = nil;

    if (pickerView.tag == 0) {
        title = [NSString stringWithFormat:@"%d", (200 + (item * 20))];
    }
    else if (pickerView.tag == 1) {
        title = [NSString stringWithFormat:@"%d", item + 1];
    }

    return title;
}

我在這里做錯了什么?

您要在方法或函數中聲明選擇器視圖嗎? 這意味着它在其他方法或函數中不可見。 您可能需要將其設置為屬性,或者至少在類擴展中將其聲明為實例變量。

標簽的默認值為零,最好將標簽計數從1開始, 然后您將能夠知道它是在其他位置設置還是未正確設置。

if (indexPath.section == 0) {
    pickerView.tag = 1;
}
else if (indexPath.section == 1) {
    pickerView.tag = 2;
}

在將標簽添加到視圖之后,將其設置為pickerView。 現在,視圖上的pickerView是原始實例的副本。 將標記代碼移到addSubView調用上方

暫無
暫無

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

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