我在 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 并重新开始计数。为什么??有任何想法吗??