Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Legolover64

macrumors regular
Original poster
Feb 24, 2008
149
0
I have an NSMutableArray of UIImageViews that have been added as subviews of a particular view.

When I replace a view like this:

Code:
    UIImageView *tempview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
    tempview.frame = CGRectMake(0, 0, 667, 920);
    [drawViewController.drawImage replaceObjectAtIndex:0 withObject:tempview];

I can see that the view is replaced, but doesn't redraw. Occasionally, this will cause the application to become unresponsive. I've tried stepping through the functions, but that doesn't seem to help. I tried `setNeedsDisplay` but that doesn't seem to work either. Is there something else I'm missing?
 
I'm a little confused. You've added the image to an NSMutableArray I'm guessing, but you haven't added it to any view or contentView.
 
xStep,

I guess I'm a little confused about what exactly is "inside" of my NSMutableArray.

Here's how the array gets initialized:

Code:
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
UIImageView *l2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"transparent.png"]];
UIImageView *l3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"transparent.png"]];
UIImageView *l4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"transparent.png"]];
drawImage = [NSMutableArray arrayWithObjects:bg, l2, l3, l4, nil];
for(int i = 0; i < 4; i++)
{
	[self.view addSubview:[drawImage objectAtIndex:i]];
}

Am I right in thinking that any change to the drawImage array also effects self.view? Or no? If so, how would I do things like switching out UIImageViews in both the array and the UIView?
 
Changing the contents of the array will not update the view that's visible. Why don't you change the image in the image view rather than make a new view?

Code:
imageview.image = myUIImage;
 
Changing the contents of the array will not update the view that's visible. Why don't you change the image in the image view rather than make a new view?

Code:
imageview.image = myUIImage;

Sadly, I can't seem to actually change the aspects of an object within an NSMutableArray for whatever reason, which I think lead me to my original concern. For example, this line doesn't compile:

Code:
[[drawViewController drawImage]objectAtIndex:0].image = [UIImage imageNamed:@"background.png"];

Am I using the NSMutableArray wrong?
 
Code:
[[drawViewController drawImage]objectAtIndex:0].image = [UIImage imageNamed:@"background.png"];

Am I using the NSMutableArray wrong?
No, it's more like you are using Objective-C wrong. objectAtIndex: is going to return an id. id has no idea that it is a UIImageView. Therefore, it doesn't understand the image property you are trying to access.
 
You can typecast an id to be a UIImage view
Yes, of course. I just didn't come out and say that directly because I was hoping the OP would come to that discovery on their own. I believe it is more satisfying (and more memorable) if one gets their own eureka moments rather than just being told the answer. Hints > copy&paste code.
 
I'm really not big fan of this kind of nested code. I would probably write this in two or three lines but you can do it like this:

Code:
[[[drawViewController drawImage]objectAtIndex:0]setImage:[UIImage imageNamed:@"background.png"]];

without a typecast or a compiler error/warning. The compiler doesn't know everything it probably should when it comes to properties.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.