我有按钮:
...
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
rightButton.tag = myCustomNumber;
[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
...
这是IBAction:
..
-(IBAction)showDetails:(id)sender{
// here I want to NSLOG button tag
}
...
怎么做?
答案 0 :(得分:11)
将您的发件人转发给UIControl
-(IBAction)showDetails:(UIControl *)sender {
// here I want to NSLOG button tag
NSLog(@"%d",sender.tag);
}
答案 1 :(得分:3)
如果始终从UIButton
调用showDetails,您可以将方法的名称更改为:
- (IBAction)showDetails:(UIButton *)sender {
NSLog(@"%i", (UIButton *)sender.tag);
}
请记住也在界面文件
上执行此更改但是,如果您使用来自不同IBAction元素的showDetails,则必须进行内省并检查发件人是否为UIButton:
- (IBAction)showDetails:(id)sender {
if ([sender isKindOfClass:[UIButton class]]
NSLog(@"%i", (UIButton *)sender.tag);
}
编辑:执行此操作的原因是,在编写代码的方式中,发件人具有动态类型id
,并且没有任何tag
属性
答案 2 :(得分:2)
NSLog("%d", (UIButton *)sender.tag);
sender是一个UIButton对象。希望能帮助到你。快乐的编码:)