
给button添加事件,调用的函数带参数,怎么传参数
1个回答
2015-12-13 · 知道合伙人软件行家
关注

展开全部
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, buttonY, width, height)];
//给button添加点击事件,action参数中写入事件执行方法
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
//在button的tag中添加你需要传递的参数,目前资料中只有这种方法
//你可以传入任意类型的参数
[button setTag:100];
//下面是action方法
-(void)action:(id)sender{
//这个sender其实就是UIButton,因此通过sender.tag就可以拿到刚才的参数
int i = [sender tag];
}
//给button添加点击事件,action参数中写入事件执行方法
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
//在button的tag中添加你需要传递的参数,目前资料中只有这种方法
//你可以传入任意类型的参数
[button setTag:100];
//下面是action方法
-(void)action:(id)sender{
//这个sender其实就是UIButton,因此通过sender.tag就可以拿到刚才的参数
int i = [sender tag];
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询