按钮标签不能在循环中使用动态值?

Button tag not working with Dynamic Value in a loop?

请检查我的代码:

for (i = 0; i < fifth_img.count; i++)
{
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    aButton.frame     = CGRectMake(xCord, yCord + space,buttonWidth,buttonHeight);
    [aButton addTarget:self action:@selector(whatever1:) forControlEvents:UIControlEventTouchUpInside];
    [aButton setTitle:[NSString stringWithFormat:@"%d",(int)i] forState:UIControlStateNormal];
    aButton.titleLabel.font = [UIFont systemFontOfSize:0.0];

    aButton.tag = i;
    [aButton setBackgroundImage:[UIImage imageNamed:[fifth_img objectAtIndex:i]] forState:UIControlStateNormal];
    [seasonsScrollView addSubview:aButton];
    yCord += buttonHeight + space;
}
[seasonsScrollView setContentSize:CGSizeMake(Vu_leftPanel.frame.size.width, yCord)];
[Vu_leftPanel addSubview:seasonsScrollView];


for( int b=0; b<6; b++){
    [(UIButton *)[Vu_leftPanel viewWithTag:b] setBackgroundImage:[UIImage imageNamed:[fifth_img objectAtIndex:b]] forState:UIControlStateNormal];
}

当我在 viewWithTag:5 中输入 5 或 3 或 1 时,它可以正常工作,但当我只输入 b 时,应用程序崩溃了。

int xCord = 0;
    int yCord = 0;
    int space = 5;
    int buttonWidth = 25;
    int buttonHeight = 25;
    for (int i = 0; i < 5; i++)
    {
        UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
        aButton.frame     = CGRectMake(xCord, yCord + space,buttonWidth,buttonHeight);
        [aButton addTarget:self action:@selector(whatever1:) forControlEvents:UIControlEventTouchUpInside];

        [aButton setTitle:[NSString stringWithFormat:@"%d",(int)i] forState:UIControlStateNormal];
        aButton.titleLabel.font = [UIFont systemFontOfSize:0.0];

        aButton.tag = 1000 + i;
        [aButton setTitle:[NSString stringWithFormat:@"%i",1000 + i] forState:UIControlStateDisabled];
        [aButton setBackgroundImage:[UIImage imageNamed:[fifth_img objectAtIndex:i]] forState:UIControlStateNormal];
        [seasonsScrollView addSubview:aButton];
        yCord += buttonHeight + space;
    }
for( int b=0; b<6; b++){
        [(UIButton *)[Vu_leftPanel viewWithTag:1000 - b] setBackgroundImage:[UIImage imageNamed:[fifth_img objectAtIndex:b]] forState:UIControlStateNormal];
    }

确保 Vu_leftPanel 没有任何其他视图的标签介于 0 和 6 之间 [因为视图的默认标签是 0]。由于 setBackgroundImage:forState: 方法只能在 UIButton 上调用。 如果任何其他视图接收到此方法调用,则应用程序将崩溃,无法识别的选择器发送到实例异常。

尝试将标签设置为 1000 到 1006。然后尝试设置背景图片。