我有这段代码:
- (IBAction) checkIt:(id)sender{
NSString *button = [[(UIImageView *)sender label]text];
我想获得UIImageView的标签。我究竟做错了什么?这适用于按钮...不确定我理解如何正确访问属性。
答案 0 :(得分:1)
子类UIImageView
。添加字符串成员和属性以进行访问。不是创建UIImageView
的对象,而是创建类的对象。
在.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
在您要使用此类的文件中
-(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];
}
答案 1 :(得分:0)