Objective c 基本目标C代码问题

Objective c 基本目标C代码问题,objective-c,Objective C,在我的网格类中,我有一个构造函数: - (id) init { if ( self = [super init] ) { isOccupied = NO; gridImage = [[UIImage alloc] initWithContentsOfFile:@"grid.png"]; } return self; } 我将gridImage设置为属性 在我的viewController中,我有: UIImageView

在我的网格类中,我有一个构造函数:

 - (id) init
{
    if ( self = [super init] )
    {

        isOccupied = NO;
        gridImage = [[UIImage alloc] initWithContentsOfFile:@"grid.png"];


    }
    return self;
}
我将gridImage设置为属性

在我的viewController中,我有:

UIImageView *temp = [[UIImageView alloc]initWithFrame:CGRectMake(10,10,200,200)];
[temp setImage: myGrid.gridImage];
[self.view addSubview: temp];
myGrid是Grid类的一个实例。。。 但它不显示?当我直接输入文件名时,它会工作,但当我使用属性访问时,它不会工作。有人能帮忙吗

我的网格标题:

@interface Grid : NSObject {


//Locations *locations[20][20];
BOOL isOccupied;
UIImage *gridImage; 
//Locations *mergedlocations[20][20];   


}
@property (retain) UIImage *gridImage

)

你能用retain来设置图像吗

gridImage = [[UIImage imageNamed:@"grid.png"] retain];

“当我直接输入文件名”是什么意思?在视图控制器中还是在网格类中?在视图控制器中:)谢谢回复!我还没有真正玩弄过objective C,但是
UIImage alloc
不应该在你的
alloc
方法中吗?@mathepic-不,alloc很好,他正在他的构造函数中实例化一个UIImage对象。@John-在你的视图控制器中,在[temp setImage…NSLog(@“temp is%@”,temp)之后添加这一行;它在日志中告诉了你什么?它起作用了!:)。你能告诉我出了什么问题吗?谢谢!initwithcontents文件需要文件的绝对路径(我想)