简体   繁体   中英

Accessing/setting UIImageView from array

Having issues with this, I'm certain that I'm not grasping something.

Let's say I've created properties for three image views and linked those views to three views in Interface Builder. I synthesize them, link them all in IB (double checked). I've also created a property for a NSMutableArray and synthesized it.

When the view loads (viewdidload), I put all of the aforementioned image views into the array. For example:

[imageArray initWithObjects: img1, img2, img3, nil];

How do I directly access/set/change/whatever the views directly from the array?

For instance, if I wanted to change what img1 is displaying, I've been trying things like:

[imageArray objectAtIndex:0].image = [UIImage imageWithName:@"someimage.png"];

But it gives me an error. If I replace img1 in the array, will it display in IB?

Try this

UIImageView *selectedImageView = (UIImageView *)[imageArray objectAtIndex:0];
selectedImageView.image = [UIImage imageWithName:@"someimage.png"];

QueueOverflow's solution is correct. In addition to that, u can also do like,

((UIImageView *)[imageArray objectAtIndex:0]).image = [UIImage imageNamed:@"someimage.png"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM