views:

37

answers:

1

How do you make sure that the UIButton tag is not overwritten by the previous tag?

for example:

(IBAction)addButton:(id)sender {

if (buttoncount == 2) return
buttoncount++

UIButton *newButton
...
// Should I change this to setTag?
newButton.tag = buttoncount;
   // This line doesn't seem to work
newbutton.tag = newButtonCount NSInteger(buttoncount);
...

[self.view addSubview:newButton];

}

Thanks in advance

A: 

This is not Objective-C:

newbutton.tag = newButtonCount NSInteger(buttoncount);

Remove that line, and your code should work as expected because buttonCount is incremented each time you add a button. The first button will be tagged 1, the second button will be tagged 2, and after that it seems that your code will prevent more than two buttons.

Arseniy Banayev