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

hellomoto4

macrumors 6502a
Original poster
Jul 11, 2008
804
0
Australia
I am attempting at turning some code written for Mac OS X into an iOS application. This is the working code for OS X:
Code:
// To create the images on the view:
NSRect imageRect = NSMakeRect(24+(i*80), ypos, 72, 96);
    NSImageView *theImageView=[[NSImageView alloc] initWithFrame:imageRect];
    [theImageView setBounds:imageRect];
    [self addSubview:theImageView];
    [theImageView setImage:[tCard image]];
    [self.imageviews addObject:theImageView];

// To remove the images on the view:
    for (NSImageView *timageview in self.imageviews) {
    [timageview removeFromSuperview];
}
[self.imageviews removeAllObjects];

At the moment, I have:
Code:
//To create the image on the view
UIImageView * theImageView = [[UIImageView alloc] initWithFrame:CGRectMake(24+(i*80), ypos, 72, 96)];
    [self addSubview:theImageView];
    [theImageView setImage:[tCard image]];
    [self.imageviews addObject:theImageView];

// To remove the images on the view:
 for (UIImageView *timageview in self.imageviews) {
    [timageview removeFromSuperview];
}
[self.imageviews removeAllObjects];

However! The removing of images does not work. The images are shown fine on the view, but I cannot remove them. To be totally honest, I'm not 100% sure about how the sample code completely works, but I am hacking away and trying to work it out, hence why I seem to be sort of half way there, but can't get the images to remove :'(

I appreciate your following input! Any questions or more information required let me know.


Note: This is how self.imageviews is initialized:
Code:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
    self.imageviews = [[NSMutableArray alloc] initWithCapacity:4]; 
}
return self;
}
 
The removing of images does not work.

Nothing jumps out at me so if you haven't done it, place NSLog statements before the removal loop and within it to verify that it is actually executing. Or use break points to the same affect.

At this point I suspect you are not executing that removal code or that the imageviews array object is nil.
 
Have you tried using a more common C for or while loop?

Personally, I rarely use loops other than those...
 
Nothing jumps out at me so if you haven't done it, place NSLog statements before the removal loop and within it to verify that it is actually executing. Or use break points to the same affect.

At this point I suspect you are not executing that removal code or that the imageviews array object is nil.

Sorry, should have mentioned that I've used breakpoints and nothing. It would seem that the problem is in the for loop, but I just can't put my finger on it.


Have you tried using a more common C for or while loop?

Personally, I rarely use loops other than those...

I've tried to incorporate different loops but to no avail. Perhaps my code was sketchy though. What would you recommend to use?
 
I've tried to incorporate different loops but to no avail. Perhaps my code was sketchy though. What would you recommend to use?

Either use breakpoints or NSLogs in your loop. Put one before entering the loop, one inside the loop before anything happens, and another in it after the removal occurs, and then one more entirely outside the loop.

That should let you know exactly what is causing the crash.

One possibility... When remove is called, the index of each other image will be changed. Thus if your loop is calling different indexes each time, it could mess up. Imagine, for example, you have three images A, B, and C.

Loop is entered, the image at index 0 is removed. Thus A is gone, B is now at index 0, and C is at index 1. The image at index 1 is removed, thus C is gone and B is all that's left, at index 0. The image at index 2 is removed, but, hey wait a minute, there's nothing at index 2, so crash.

There are two possible ways of solving this that come to mind:

1 - Get the count and start from the end and work your way back through the array, thus indexes won't change on you.
2 - Use a while loop. Just check each time that there are any subviews (count will work) and remove any image (at index 0 should always work, if it has been confirmed there is a subview.)

I haven't actually checked to see if this works (I'm on an iPhone, not a computer with Xcode), but I feel like I have a memory of having to have done this over a year ago on Egg Drop!...
 
Sorry, should have mentioned that I've used breakpoints and nothing.

Please explain. Do you mean the breakpoint wasn't being hit? If that's what happened, then the code isn't being executed. You need to look earlier in your code. And try posting complete code, in complete methods.

In general, please do the following:
1. Describe what you expected to happen.
2. Describe what actually happened.
3. Post complete code, not isolated fragments.

Example: "I set breakpoints and expected the program to stop when one was hit. The program didn't stop at any breakpoint. It didn't stop at all."


I've tried to incorporate different loops but to no avail. Perhaps my code was sketchy though. What would you recommend to use?
Post your code. If by "sketchy" you mean "possibly wrong", how could we determine whether it's right or wrong except by seeing the actual code?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.