如何在屏幕上显示 UIControl 的子类

发布于 2024-11-14 12:41:07 字数 1623 浏览 4 评论 0原文

我通过以下代码创建了一个名为 ToggleImageControl 的 UIControl 子类(参考以下帖子 复选框图像在 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 的实例,但无法显示“正常图像”。我的下面的实现中是否缺少某些内容?

//In a UItableview cell
ToggleImageControl *toggleControl = [[ToggleImageControl alloc]initWithFrame:CGRectMake(670, 10,80, 80)];

[cell.contentView addSubview:toggleControl];

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

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];

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

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

发布评论

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

评论(1

溺孤伤于心 2024-11-21 12:41:07

确保在 initWithFrame: 方法中调用 [super initWithFrame:frame]

前任

self = [super initWithFrame:frame];
if (self) {

  // Initialization

}

return self;

Make sure to call [super initWithFrame:frame] in your initWithFrame: method.

e.x.

self = [super initWithFrame:frame];
if (self) {

  // Initialization

}

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