将标签分配给UIButton

时间:2012-07-11 17:11:18

标签: objective-c xcode

我在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次,因此我增加(从1到9)。我知道我正确增加,因为我以相同的方式为图像分配标签,但由于某种原因,NSLog显示:

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

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

1 个答案:

答案 0 :(得分:0)

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

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

z = 0;
视图中的

确实加载了。我仍然无法理解为什么我没有工作,因为我正确地将标签分配给图像,并且我以相同的方式进行...