xcode uibutton 发件人标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1088481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:38:59  来源:igfitidea点击:

uibutton sender tag

iphonexcode

提问by Bramble

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?

我有一个 UIImageView 对象,单击它会播放动画,我想重用相同的代码来制作多个对象。我如何设置发件人标签,以便它知道它是一个不同的对象?

.h

。H

- (IBAction)startClick:(id)sender;

.m

.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];
}

采纳答案by Ken Pespisa

The sender is the object that called the startClick method. You can cast that object into a UIImageView and then look at that object's tag property to determine which one it is.

发送方是调用 startClick 方法的对象。您可以将该对象转换为 UIImageView,然后查看该对象的 tag 属性以确定它是哪一个。

You'll need to set the tag property elsewhere in the code. If you have the UIImageViews in Interface Builder, you can use the properties window to enter a tag number. Otherwise, when you allocate and init your UIImageViews, set the tag property then.

您需要在代码的其他地方设置 tag 属性。如果在 Interface Builder 中有 UIImageViews,则可以使用属性窗口输入标签编号。否则,当您分配和初始化 UIImageViews 时,请设置标签属性。

回答by Rose Perrone

Use [sender tag].

使用[sender tag].

Why not sender.tag, you ask?

sender.tag你问为什么不呢?

You can only use the dot notation if you cast senderas an instance of UIView, as in ((UIView *)sender).tag. Objects of UIViewhave a tag property. If you don't cast senderas an instance of UIView, it is just an idthat conforms to the NSURLAuthenticationChallengeSenderprotocol, and it lacks a tagproperty.

如果你投你只能用点号sender作为实例UIView,如((UIView *)sender).tag。的对象UIView具有标签属性。如果不sender作为 的实例进行强制转换UIView,则它只是id符合NSURLAuthenticationChallengeSender协议的an ,并且缺少tag属性。

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
    }
}