如何在屏幕上显示 UIControl 的子类 [英] How to display a Subclass of UIControl on the screen
本文介绍了如何在屏幕上显示 UIControl 的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过以下代码创建了一个名为 ToggleImageControl 的 UIControl 子类(参考以下帖子 UITableViewCell 中的复选框图像切换)
I have created a subclass of UIControl called ToggleImageControl via the following code (with reference to the following post Checkbox image toggle in UITableViewCell)
@interface ToggleImageControl : UIControl
{
BOOL photoIsStarred;
UIImageView *imageView;
UIImage *normalImage;
UIImage *selectedImage;
}
@property (nonatomic, assign) BOOL photoIsStarred;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIImage *normalImage;
@property (nonatomic, retain) UIImage *selectedImage;
- (void) toggleImage;
@end
@implementation ToggleImageControl
@synthesize photoIsStarred, imageView, normalImage,selectedImage;
- (id)initWithFrame:(CGRect)frame
{
NSLog(@"in inti with frame method");
self.normalImage = [UIImage imageNamed: @"blank_star.png"];
self.selectedImage = [UIImage imageNamed: @"star.png"];
self.imageView = [[UIImageView alloc] initWithImage: normalImage];
// set imageView frame
[self addSubview: imageView];
[self addTarget: self action: @selector(toggleImage) forControlEvents: UIControlEventTouchDown];
return self;
}
- (void) toggleImage
{
NSLog(@"image toggled");
self.photoIsStarred = !photoIsStarred;
self.imageView.image = (photoIsStarred ? selectedImage : normalImage);
}
@end
我尝试在表格单元格中创建 ToggleImageControl 的实例,但无法显示正常图像".下面我的实现中是否遗漏了什么?
I tried to create an instance of ToggleImageControl in a table cell but was not able to display the 'normal image'. Is there something missing from my implementation below?
//In a UItableview cell
ToggleImageControl *toggleControl = [[ToggleImageControl alloc]initWithFrame:CGRectMake(670, 10,80, 80)];
[cell.contentView addSubview:toggleControl];
推荐答案
确保在您的 initWithFrame:
方法中调用 [super initWithFrame:frame]
.
Make sure to call [super initWithFrame:frame]
in your initWithFrame:
method.
例如
self = [super initWithFrame:frame];
if (self) {
// Initialization
}
return self;
这篇关于如何在屏幕上显示 UIControl 的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文