2014-07-20 76 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