UIView setNeedsDisplay不起作用

时间:2013-03-11 08:35:21

标签: ios uiview setneedsdisplay

简单代码在这里:

自定义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];
}

ViewController.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
}

按下按钮时,图像视图不会改变。我怎样才能解决这个问题? 非常感谢你!

2 个答案:

答案 0 :(得分:1)

首先,你不需要setNeedsDisplaysetNeedsDisplay用于重绘视图(如果需要,它会在内部调用视图的drawRect:方法),这不是您想要的。

其次,您已将按钮的类命名为MyButton,但在实际的.h和.m文件中,它分别为@interface PaiLifeReadingListButton@implementation PaiLifeReadingListButton

另一方面,视图和按钮未使用框架初始化。但我认为你已经这样做了,因为你唯一的问题是图像视图没有改变。

答案 1 :(得分:0)

在按下方法的代码中,您应该更改层次结构中的视图,但只更改对象成员。你可以做点什么

myView.myButton.logImageView.image = [UIImage imageNamed:@"other-pic"];