2014-07-20 77 views
-2

我有一个对象阵列设置是这样的:如何使用对象的对象阵列内部在物镜Ç

NSMutableArray *image_view_array; 
IBOutlet UIImageView *image_view_1, *image_view_2, *image_view_2; 

和:

image_view_array = [[NSMutableArray alloc] initWithCapacity:3]; 
[image_view_array addObject:image_view_1];  
[image_view_array addObject:image_view_2]; 
[image_view_array addObject:image_view_3]; 

如何访问图像视图阵列的图像? 我尝试这些:

image_view_array[0].image 
[image_view_array objectAtIndex:0].image 

,但它不工作。 谢谢!

+2

当你说它不起作用时,你会得到什么错误? –

+0

代码中的每个代码片段都位于哪里? – vikingosegundo

回答

0

你只需将它转换回的UIImageView:

((UIImageView *)[image_view_array objectAtIndex:0]).image 

的NSArray中不保留类型,所以你拉出来一切都将是一个NSObject至于编译器知道。所以你作为编程人员必须将其重新投入输入类型。

+2

实际上,你从数组中取出的所有东西都是'id',而不是'NSObject *'。 – rmaddy

1

而不是铸造,您可以发送消息,因为输入对象id确实接受每个消息,数组中的对象保持为id

UIImage *img = [image_view_array[0] image]; 
1

两行更容易阅读和调试:

UIImageView *imageView = image_view_array[0]; 
UIImage *image = imageView.image; 

BTW - 学会使用标准的命名约定。骆驼案件优于使用下划线。

将您的数组命名为imageViewArray,并将您的网点命名为imageView1imageView2