iPhone - update the property of nth object of a NSMutablearray
Probably its my first post here. I have an NSMutableArray *iconsMutableArray; And have added some UIImageView in the NSMutableArray.
for (int i = 2; i <= 9; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"Guide-Icon-%d.png", i]]];
[iconsMutableArray addObject:imageView];
[imageView release];
}
Now i want to change the UIImage so开发者_Python百科urce of Nth object in the UIImageView. How can I? I have tried several ways e.g,
[[iconsMutableArray objectAtIndex:activeItem] image] = [UIImage imageNamed:@"Guide-Icon-7_Active.png"];
Can anyone please help me?
[[iconsMutableArray objectAtIndex:activeItem] setImage:[UIImage imageNamed:@"Guide-Icon-7_Active.png"]];
Welcome to StackOverflow!
You are accessing the image property in your code sample. You should use setter for the property like this:
UIImage *newImage = [UIImage imageNamed:@"Guide-Icon-7_Active.png"]
[[iconsMutableArray objectAtIndex:activeItem] setImage:newImage];
Looks like you're very new to ObjectiveC and iOS programming, right? Unless you start with a few ObjC tutorials you'll run into many more of these questions. Better learn the basics, first
Here's a way how to solve this:
Look up the documentation for NSMutableArray (in Xcode 4, open the Organizer window, then switch to "Documentation", in Xcode 3 press Cmd-?). There you'll find the methods you can use. One is called "replaceObjectAtIndex:withObject:". That's the one you'd want in here.
Also, as you're new to StackOverflow, do not forget to click on the checkmark on the left of the best answer so that this question of yours gets marked as "answered".
Good luck with your iOS development, it can be quite some fun!
You can try below code
[iconsMutableArray replaceObjectAtIndex:7 withObject:[UIImage imageNamed:@"Guide-Icon-7_Active.png"]]; // 7- thats your object index
精彩评论