为 UIButton - iOS 添加目标



>Anyoone可以帮助我如何使用UIButton添加操作以调用以下方法。

-(void)navigatePics:(id)sender andIndex:(NSInteger *)index{}

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

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

然后在导航图片中,阅读标签。

-(void)navigatePics:(UIButton *)sender
{
 int index = sender.tag;
 // index = 1234;
}
UIButton *button = [[UIButton alloc] init];
button.tag = 4; //index
[button addTarget:self @selected(buttonDidTap:)];
...
[button release];
-(void)buttonDidTap:(UIButton *)sender{
NSInteger index = sender.tag;
}

这应该可以解决问题。如果要在另一个实例上调用此方法,请不要使用 self。在这种情况下,只需使用该实例代替 self。

[button addTarget:self action:@selector(navigatePics:andIndex:) forControlEvents:UIControlEventTouchUpInside];

最新更新