I have button:
...
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
rightButton.tag = myCustomNumber;
[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
...
And here is IBAction:
..
-(IBAction)showDetails:(id)sender{
// here I want to NSLOG button tag
}
...
How to do that?
Just cast your sender to UIControl
-(IBAction)showDetails:(UIControl *)sender {
// here I want to NSLOG button tag
NSLog(@"%d",sender.tag);
}
If showDetails is always called from a UIButton
you could change the name of the method to:
- (IBAction)showDetails:(UIButton *)sender {
NSLog(@"%i", (UIButton *)sender.tag);
}
Remember to perform this change also at the interface file
However, if you use showDetails from different IBAction elements you will have to introspect and check if sender is a UIButton:
- (IBAction)showDetails:(id)sender {
if ([sender isKindOfClass:[UIButton class]]
NSLog(@"%i", (UIButton *)sender.tag);
}
Edit: The reason of doing this is that in the way you wrote the code, sender has a dynamic type id
and it doesn't have any tag
property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With