制定者在我UIView
子類.H:getter和對UIView的
@interface MyView : UIView
@property (nonatomic, strong) UIImageView *imageView;
@end
我設置了我的UIView
子類UIImageView
像這樣(init
法):
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 0, 34, 34)];
self.imageView.layer.cornerRadius = CGRectGetWidth(imageView.frame)/2.0f;
self.imageView.layer.masksToBounds = YES;
self.imageView.backgroundColor = [UIColor someColor];
[self addSubview: self.imageView];
所以,我可以做myView.imageView.image = [UIImage someImage]
,它正確設置了UIImageView
的圖像。我試圖清理我的UIView
子類中的代碼時出現問題。
我試圖做到這一點:
- (UIImageView *)imageView {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 0, 34, 34)];
imageView.layer.cornerRadius = CGRectGetWidth(profilePhotoImageView.frame)/2.0f;
imageView.layer.masksToBounds = YES;
imageView.backgroundColor = [UIColor redColor];
return imageView;
}
,然後從init
方法做
[self addSubView: [self imageView]];
但是當我做myView.imageView.image = [UIImage someImage]
圖像不上的UIImageView顯示。
我忘了什麼?
你能告訴你的執行'imageView' getter方法的休息嗎? – Gavin
請參閱我的回答,瞭解如何根據更新後的問題更改解決問題的方法。 – Gavin
必須檢查ivar是否已經被初始化或者沒有...如果不只是像if(!_ imageView){_ imageView = [UIImageView alloc] init]那樣創建;} –