I’ve created 5 buttons dynamically, as follows:
float xpos=0,ypos=0;
for (int i = 0; i < 5; i++)
{
but = [UIButton buttonWithType:UIButtonTypeCustom];
[but setTag:i];
[but setImage:[UIImage imageNamed:@"btfrnt.png"] forState:UIControlStateNormal];
[but setFrame:CGRectMake(xpos, ypos, 40, 40)];
xpos+=80;
[but addTarget:self action:@selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];
}
In Click event of any of the button, I want to find the position of the button which I have clicked….
-(void)checkboxButton:(UIButton*)sender
{
NSLog(@"%d",xpos);
}
But, it displays only the xpos of the last button…Is there any way to identify it by means of its tag?
I take it xPos is a global variable – of course it contains the value of the last button, that’s the value it was set to last.
For the button position you don’t need to store anything in a global variable – just get it from the sender object that is delivered to you in the click event handler – it is a UIButton pointer, and the UIButton object has a frame structure with origin.x and origin.y, as well as a size.width and size.height, by the way.