How to display a Subclass of UIControl on the screen

扶醉桌前 提交于 2019-12-11 00:37:53

问题


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

回答1:


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

e.x.

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

  // Initialization

}

return self;


来源:https://stackoverflow.com/questions/6247290/how-to-display-a-subclass-of-uicontrol-on-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!