为UIButton添加目标 - iOS

Sid*_*kan 14 uibutton ios

Anyoone可以帮助我如何使用添加动作来调用以下方法UIButton.

-(void)navigatePics:(id)sender andIndex:(NSInteger *)index{}
Run Code Online (Sandbox Code Playgroud)

CSm*_*ith 53

使用button.setTag:(NSInteger)方法将索引作为标记添加到UIButton.

UIButton *button = ...;
[button setTag:1234];
[button addTarget:self action:@selector(navigatePics:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)

然后在navigatePics中,阅读标签.

-(void)navigatePics:(UIButton *)sender
{
 int index = sender.tag;
 // index = 1234;
}
Run Code Online (Sandbox Code Playgroud)