如何UIButton添加事件并传递参数

转自

http://blog.csdn.net/wherejaly/article/details/5563506 


我们以UIButton为demo

[c-sharp]  view plain copy
  1. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, buttonY, width, height)];  
  2. //给button添加点击事件,action参数中写入事件执行方法  
  3. [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];  
  4. //在button的tag中添加你需要传递的参数,目前资料中只有这种方法  
  5. //你可以传入任意类型的参数  
  6. [button setTag:100];  
  7. //下面是action方法  
  8. -(void)action:(id)sender{  
  9. //这个sender其实就是UIButton,因此通过sender.tag就可以拿到刚才的参数  
  10.     int i = [sender tag];  
  11. }  
 

目前暂时没有找到其他方法来传递参数,

你可能感兴趣的:(如何UIButton添加事件并传递参数)