Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uibutton sender tag

Tags:

xcode

iphone

I have a UIImageView object that when clicked it will play a animation, I want to reuse the same code to make multiple objects. How do I set the sender tag so it knows its a different 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];
}
like image 908
Bramble Avatar asked Dec 01 '22 12:12

Bramble


1 Answers

Use [sender tag].

Why not sender.tag, you ask?

You can only use the dot notation if you cast sender as an instance of UIView, as in ((UIView *)sender).tag. Objects of UIView have a tag property. If you don't cast sender as an instance of UIView, it is just an id that conforms to the NSURLAuthenticationChallengeSender protocol, and it lacks a tag property.

Here's an example of using a button's 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
    }
}
like image 154
Rose Perrone Avatar answered Dec 05 '22 23:12

Rose Perrone