1

Here is some code for iPhone:

Controller.h:

IBOutlet UIImageView *userImage;
IBOutlet UIImageView *userImage2;

}
@property (nonatomic, retain) IBOutlet UIImageView *userImage;
@property (nonatomic, retain) IBOutlet UIImageView *userImage2;

Controller.m:

UIImageView *myImage2b = [[UIImageView alloc] initWithFrame:myImageRect4];
[myImage2b setImage:[UIImage imageNamed:@"RedPin.png"]];
[userImage2 addSubview:myImage2b];

[userImage2 release];
[myImage2b release];

I am just trying to get rid of the "RedPin" from my Subview and re-use userImage2 for future pins. Of course, userImage2 is not accessible after the release. This is not a mapping app. Any ideas would be helpful. Thanks in advance.

Raphael Schweikert
  • 18,244
  • 6
  • 55
  • 75
enegene
  • 31
  • 1

1 Answers1

0

What I would do is declare userImage2 in your header file., like you have. And then instead of releasing it after initializing it simply add the

[userImage2 release]

to your dealloc function and call

[userImage2 removeFromSuperview]

whenever you want to remove the image from the view.

EDIT: I seem to have misread your post slightly. If you want to remove just the red pin from userImage2 use:

[[userImage2.subviews objectAtIndex:0] removeFromSuperview]
Brenton Morse
  • 2,567
  • 4
  • 21
  • 16
  • Thanks Brenton - I have tried the removeFromSuperview before. I have Case 1 to Case 5. When I addSubview an image to userImage2 in Case 1 and then add the removeFromSuperView in Case 5, userImage2 gets removed from Case 1 even before I enter Case 5. Any ideas? – enegene Mar 09 '11 at 15:14
  • @enegene - I'd need a little more info on the nature of the switch statement, how and when it's being called a such, before I can be of much help here. If you could show the code I could be a little more helpful. – Brenton Morse Mar 09 '11 at 15:32
  • Thanks Brenton - I have been doing a little more research and found out that release and autorelease does not do it immediately. Therefore I have the addSubView on my next setImage. I will look into it a bit more. – enegene Mar 09 '11 at 19:59
  • Hello Brenton, I just noticed your edit - [[userImage2.subviews objectAtIndex:0] removeFromSuperview]. If I use this where the userImage2.subview was created, the red pin is not shown. If I use it in another view where I do not want the pin, it works, but if I hit the IBAction button more than once for that view, it tries to removeFromSuperview again and I crash. I will try to set a count and not initiate the removeFromSuperview twice. Thanks – enegene Mar 09 '11 at 20:46
  • @enegene No problem. It's crashing because the subviews array is empty after removing the object at index 0. You are trying to remove something that isn't there. You can try using a [userImage2.subviews count] to make sure there is something there before removing. – Brenton Morse Mar 09 '11 at 21:28
  • Brenton, Thanks for all your help. I have found the perfect solution from your ideas: while([[userImage2 subviews] count]) { [[[userImage2 subviews] lastObject] removeFromSuperview]; } – enegene Mar 10 '11 at 18:30
  • @enegene Not a problem. Glad I could help. – Brenton Morse Mar 10 '11 at 21:13