自定义UIButton不能与链接的UIImageView进行交互 [英] Custom UIButton cant interact with linked UIImageView
问题描述
我坚持下面的代码. 我的UIButton Extended类无法显示或隐藏UIImageView的一些方式
I'm stuck on the following code. Some how my UIButton Extended class cant show or hide an UIImageView
正在调用我的方法,并且imageview不是nil.
My methods are being called and the imageview is not nil.
这是代码:
@interface UILinkedImageButton : UIButton {
IBOutlet UIImageView *linkImageView;
}
@property (nonatomic, retain) IBOutlet UIImageView *linkImageView;
@end
#import "UILinkedImageButton.h"
@interface UILinkedImageButton ()
- (void)showImage;
- (void)hideImage;
@end
-------------------------------------------------------------------------------------------------
@implementation UILinkedImageButton
@synthesize linkImageView;
- (void) dealloc{
[linkImageView release], linkImageView = nil;
[super dealloc];
}
- (id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self){
[self addTarget:self action:@selector(showImage) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(hideImage) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:@selector(hideImage) forControlEvents:UIControlEventTouchUpOutside];
}
return self;
}
- (void)showImage
{
if(self.imageView){
NSLog(@"UILinkImageButton - showImage - currentStatus: %@", self.imageView);
self.imageView.hidden = NO;
[self.superview layoutIfNeeded];
}
}
- (void)hideImage
{
if(self.imageView){
NSLog(@"UILinkImageButton - hideImage");
self.imageView.hidden = YES;
}
}
@end
推荐答案
正如ThomasMüller在评论中提到的,我也认为这些动作应该在控制器中.
As Thomas Müller mentions in the comment i too think the actions should be in the controller.
除此之外,在代码中,您正在更改"imageView"对象的hidden属性,而在声明中创建的自定义图像视图是"linkImageView". 该代码不会引发错误,因为'imageView'是UIButton中声明的按钮的只读属性,它表示按钮图像视图不是您的linkImageView .
Apart from that, in your code you are changing the hidden property of 'imageView' object while the custom image view you have created in you declaration is 'linkImageView'. The code is not throwing an error because 'imageView' is the button's readonly property declared in UIButton and it represents the button image view not your linkImageView.
希望这会有所帮助.
谢谢, 斯瓦比尔
这篇关于自定义UIButton不能与链接的UIImageView进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!