简单的代码在这里:
自定义 MyButton.h
@interface PaiLifeReadingListButton : UIButton
@property (strong, nonatomic) UIImageView *logoImageView;
@end
自定义 MyButton.m
@implementation PaiLifeReadingListButton
@synthesize logoImageView = _logoImageView;
-(id)init
{
_logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(33.0, 20.0, 80.0, 80.0)];
[_logoImageView setImage:[UIImage imageNamed:@"pic"]];
[self addSubview: _logoImageView];
}
@end
自定义 MyView.h:
@property (strong, nonatomic) Mybutton *mybutton;
自定义 MyView.m:
-(id) init
{
myButton = [[MyButton alloc] init];
[self addSubview:myButton];
}
视图控制器.m:
@synthesize myView;
- (void)viewDidLoad
{
myView = [[Myview alloc] init];
[myView.myButton addTarget:self action:@selector(pressed:) forControlEvents:UIControlEventTouchDown];
self.view = myView;
}
-(void)pressed : (MyButton *)button
{
UIImageView *newImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"other-pic"] highlightedImage:nil];
myView.myButton.logImageView = newImageView;
[self.view setNeedsDisplay]; //----this does not work
}
当我按下按钮时,图像视图不会改变。我怎样才能解决这个问题?非常感谢!