iOS:获取图像标签

发布于 2025-01-04 13:25:44 字数 197 浏览 0 评论 0原文

我有这样的代码:

- (IBAction) checkIt:(id)sender{
    NSString *button = [[(UIImageView *)sender label]text];

我想获取 UIImageView 的标签。我做错了什么?这适用于按钮...不确定我是否理解如何正确访问属性。

I have this code:

- (IBAction) checkIt:(id)sender{
    NSString *button = [[(UIImageView *)sender label]text];

I want to get the label of the UIImageView. What am I doing wrong? This works with buttons...Not sure I understand how to access attributes correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小女人ら 2025-01-11 13:25:44

子类UIImageView。添加字符串成员和属性以访问它。不要创建 UIImageView 的对象,而是创建类的对象。

.h

@interface CustomImageView : UIImageView
{
    NSString *imageName;
}

@property (nonatomic, retain) NSString *imageName;

@end

和 .m中

@implementation CustomImageView

@synthesize imageName;

-(void) dealloc
{
    [imageName release];
    [super dealloc];
}
@end

在要使用此类的文件的

-(void) addCustomImageView
{
    CustomImageView *imgView = [[CustomImageView alloc]initWithImage:[UIImage imageNamed:@"abc.png"]];
    imgView.frame = CGRectMake(0, 0, 100, 100);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgViewtapped:)];
    [imgView addGestureRecognizer:tap];
    [tap release];
    [self.view addSubview:imgView];
    [imgView release];

}

-(NSString *) imgViewtapped:(CustomImageView *)sender
{
    return [sender imageName];
}

Subclass UIImageView. Add a string member and property for accessing it. Instead of creating an object of UIImageView, create the object of your class.

In .h

@interface CustomImageView : UIImageView
{
    NSString *imageName;
}

@property (nonatomic, retain) NSString *imageName;

@end

in .m

@implementation CustomImageView

@synthesize imageName;

-(void) dealloc
{
    [imageName release];
    [super dealloc];
}
@end

in the file where you want to use this class

-(void) addCustomImageView
{
    CustomImageView *imgView = [[CustomImageView alloc]initWithImage:[UIImage imageNamed:@"abc.png"]];
    imgView.frame = CGRectMake(0, 0, 100, 100);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgViewtapped:)];
    [imgView addGestureRecognizer:tap];
    [tap release];
    [self.view addSubview:imgView];
    [imgView release];

}

-(NSString *) imgViewtapped:(CustomImageView *)sender
{
    return [sender imageName];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文