我有一个UIImageView对象,单击它时会播放动画,我想重用相同的代码来制作多个对象.如何设置发件人标签以使其知道其不同的对象?
.H
- (IBAction)startClick:(id)sender;
Run Code Online (Sandbox Code Playgroud)
.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];
}
Run Code Online (Sandbox Code Playgroud)
Ros*_*one 17
使用[sender tag].
sender.tag你问,为什么不呢?
如果将sender其转换为实例,则只能使用点表示法UIView,如((UIView *)sender).tag.UIView具有标签属性的对象.如果你不sender作为一个实例进行强制转换UIView,那么它只是一个id符合NSURLAuthenticationChallengeSender协议的东西,它缺少一个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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16136 次 |
| 最近记录: |