0

我在 for 循环中有这段代码:

 //button Delete

        buttonDelete = [UIButton buttonWithType:UIButtonTypeCustom];
        [buttonDelete addTarget:self 
                           action:@selector(deleteMessage:)
                 forControlEvents:UIControlEventTouchDown];
        [buttonDelete setTitle:@"" forState:UIControlStateNormal];
        buttonDelete.frame = CGRectMake(680, -1, 30.0, 30.0);

        buttonDelete.tag = i;

      NSLog(@"Tag: %d", buttonDelete.tag);

        [buttonDelete setBackgroundImage:[UIImage imageNamed:@"deleteButton.png"] forState:UIControlStateNormal];

        [image1 addSubview:buttonDelete];

        if (DeleteButtonArray == nil)DeleteButtonArray = [[NSMutableArray alloc]init];
        [DeleteButtonArray addObject: buttonDelete];


        //Button Delete

此代码运行 9 次,因此 i 增加(从 1 到 9)。我知道我正确地增加了,因为我以同样的方式为图像分配了一个标签,但由于某种原因,NSLog 显示了这一点:

Tag: 0, 1, 2, 3, 0, 1, 2, 3,... 

所以它到达 3 并重新开始计数。为什么??有任何想法吗??

4

1 回答 1

0

我解决了这个问题:我只是这样做了:

z = z + 1;
        buttonDelete.tag = z;

z = 0;

在视图中确实加载了。我仍然不明白为什么我不工作,因为我用 i 正确地将标签分配给图像,并且我以同样的方式做......

于 2012-07-11T17:35:04.493 回答