[英]UIView setNeedsDisplay doesn't work
簡單的代碼在這里:
自定義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
}
當我按下按鈕時,imageview不會改變。 我怎樣才能解決這個問題? 非常感謝你!
第一件事,這里不需要setNeedsDisplay
。 setNeedsDisplay
用於重新繪制視圖(如果需要,它在內部調用視圖的drawRect:
方法)。
其次,您將按鈕的類別指定為MyButton
,但在實際的.h和.m文件中,分別將其命名為@interface PaiLifeReadingListButton
和@implementation PaiLifeReadingListButton
。
另一件事,視圖和按鈕未使用框架初始化。 但是我認為您已經這樣做了,因為您唯一的問題是圖像視圖沒有改變。
在按下方法的代碼中,您應該更改層次結構中的視圖,但是只能更改對象成員。 你可以做類似的事情
myView.myButton.logImageView.image = [UIImage imageNamed:@"other-pic"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.