识别 UIButton 的位置
我动态创建了 5 个按钮,如下所示:
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];
}
在任何按钮的 Click 事件中,我想找到我单击的按钮的位置....
-(void)checkboxButton:(UIButton*)sender
{
NSLog(@"%d",xpos);
}
但是,它只显示最后一个按钮的 xpos.. .有什么方法可以通过标签来识别它吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
try this :
我认为 xPos 是一个全局变量 - 当然它包含最后一个按钮的值,这是它最后设置的值。
对于按钮位置,您不需要在全局变量中存储任何内容 - 只需从单击事件处理程序中传递给您的发送者对象获取它 - 它是一个 UIButton 指针,并且 UIButton 对象具有一个框架结构顺便说一下,origin.x 和 origin.y,以及 size.width 和 size.height。
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.
你可以试试这个...
You may try this...