为UIButton添加目标 – iOS
任何人都可以帮助我如何添加操作来调用下面的方法使用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];
然后在navigatePics中,读取标签。
-(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。 在这种情况下,只需使用该实例代替自己。
[button addTarget:self action:@selector(navigatePics:andIndex:) forControlEvents:UIControlEventTouchUpInside];