Uibutton sender tag

I have a UIImageView object that when I click on it will play the animation, I want to reuse the same code to create multiple objects. How to set the sender tag so that it knows its other object?

.h

- (IBAction)startClick:(id)sender; 

.m

 - (IBAction)startClick:(id)sender { //UIImageView *theButton = (UIImageView *)sender.tag; bubble.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed: @"Pop_1.png"], [UIImage imageNamed: @"Pop_2.png"], [UIImage imageNamed: @"Pop_3.png"], nil]; [bubble setAnimationRepeatCount:1]; bubble.animationDuration = 1; [bubble startAnimating]; } 
+4
source share
2 answers

The sender is the object that called the startClick method. You can include this object in a UIImageView, and then see what is the property of the object tag to determine which one it is.

You need to set the tag property elsewhere in the code. If you have UIImageViews in Interface Builder, you can use the properties window to enter the tag number. Otherwise, when you select and run your UIImageViews, set the tag property.

+3
source

Use the [sender tag] .

Why not sender.tag , you ask?

You can only use dot notation if you attach sender to an instance of UIView , as in ((UIView *)sender).tag . UIView objects have a tag property. If you do not use sender as an instance of UIView , this only matches the id that conforms to the NSURLAuthenticationChallengeSender protocol and it lacks the tag property.

Here is an example of using a button tag:

 #define kButtonTag 2 - (void)viewDidLoad { // ... view setup ... UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; // ... button setup ... button.tag = kButtonTag; [super viewDidLoad]; } - (IBAction)startClicked:(id)sender { if ([sender tag] == kButtonTag) { // do something } } 
+17
source

All Articles