【发布时间】:2013-12-23 14:19:13
【问题描述】:
我有一个带有属性的视图,它应该从我的视图控制器中获取一个 NSString 并显示一个图像。如果我在视图中使用“320x213-1.png”对 locationImageFile 进行硬编码,则图像会正确显示,但如果我尝试从视图控制器设置 locationImageFile,则图像不会出现。我觉得我错过了一些非常基本的东西。旁注:我似乎可以从我的视图控制器中设置 locationTitle 属性而没有任何问题。
LocationViewController.m
#import "LocationViewController.h"
#import "LocationView.h"
@implementation LocationViewController
{
LocationView *_locationView;
}
- (void)loadView
{
CGRect frame = [[UIScreen mainScreen] bounds];
_locationView = [[LocationView alloc] initWithFrame:frame];
[self setView:_locationView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_locationView.locationTitle.text = aLocation.name;
_locationView.locationImageFile = @"320x213-1.png";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
LocationView.m
#import "LocationView.h"
@implementation LocationView
@synthesize locationTitle;
@synthesize locationImageFile;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setBackgroundColor: [UIColor blueColor]];
// Image container
UIImage *locationImage = [UIImage imageNamed:locationImageFile];
UIImageView *locationImageContainer = [[UIImageView alloc] initWithImage:locationImage];
[locationImageContainer setTranslatesAutoresizingMaskIntoConstraints:NO];
locationImageContainer.backgroundColor = [UIColor yellowColor];
[self addSubview:locationImageContainer];
// Text line
locationTitle = [[UILabel alloc]init];
[locationTitle setTranslatesAutoresizingMaskIntoConstraints:NO];
locationTitle.backgroundColor = [UIColor whiteColor];
[self addSubview:locationTitle];
}
return self;
}
@end
【问题讨论】:
-
这个问题发生在你没有硬编码的时候,告诉我们这是如何工作的,我怀疑在它传递给函数之前这个值是 nil,你检查了吗?
-
正常运行的硬编码行:
UIImage *locationImage = [UIImage imageNamed:@"320x213-1.png"];NSLog 确实显示该值为 nil。
标签: ios objective-c model-view-controller uiimageview uiimage