为UIButton添加目标 - iOS
问题描述:
任何人都可以帮助我如何添加操作以使用UIButton
调用以下方法。为UIButton添加目标 - iOS
-(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;
}
答
这应该可以做到。如果您想在另一个实例上调用此方法,请不要使用self。在这种情况下,只需使用该实例代替自己。
[button addTarget:self action:@selector(navigatePics:andIndex:) forControlEvents:UIControlEventTouchUpInside];
+0
我将如何通过索引值? – 2012-08-13 18:10:27
+0
您无法传递索引值...从发件人处获取 – 2012-08-13 20:28:47
答
UIButton *button = [[UIButton alloc] init];
button.tag = 4; //index
[button addTarget:self @selected(buttonDidTap:)];
...
[button release];
-(void)buttonDidTap:(UIButton *)sender{
NSInteger index = sender.tag;
}
请解释一下你想要什么类型的功能。你想让别人在{}之间写字来让这个方法做些什么吗?或者你需要帮助将它连接到Interface Builder(IB)? – MrHappyAsthma 2012-08-13 18:06:12
我只是在寻找[button addTarget:self action:@selector(_this space____)forControlEvents:UIControlEventTouchUpInside]; – 2012-08-13 18:11:45