以下代码中需要帮助

时间:2011-02-04 04:24:19

标签: iphone objective-c ios

我们可以将具有图像值的数组的对象的值赋给图像视图的变量,请参阅以下代码

NSArray *imgArray=[[NSArray alloc] initWithObjects:@"Bingo2.png", nil];
    UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    img.image=[imgArray objectAtIndex:0];    //line 3
    [self.view addSubview:img];

它不起作用,因为第3行

,应用程序崩溃了

请帮帮我, 非常感谢您的帮助。

3 个答案:

答案 0 :(得分:4)

您正在数组中存储NSString对象而不是图像。这就是崩溃的原因。

答案 1 :(得分:3)

使用此

img.image= [UIImage imageNamed:[imgArray objectAtIndex:0]];

而不是

img.image=[imgArray objectAtIndex:0];

如果以上不起作用

比你还可以使用这个

NSString *string = [NSString stringWithFormat:@"%@", [imgArray objectAtIndex:0]];
img.image= [UIImage imageNamed:string];

答案 2 :(得分:1)

您的数组中有一个文件名字符串,而不是图像。请改用[UIImage imageNamed:@"Bingo2.png"]