1

I'm trying to mimic the iPhone Springboard drag and drop functionality for a tile game and am currently using MatrixCells to generate UIButtons in the View initalisation and add them to the view. The MatrixCells have an order tag, which is used for their own button as well ([button setTag:[cell order]];).

When using `layoutSubviews' to cycle through the UIButtons in the view to arrange them in a grid, it works fine. The grid pops up and my dragging functionality allows me to free-drag the buttons where I will.

- (void)layoutSubviews {

int row = 0;
int col = 0;
for ( UIButton *button in [self subviews] ) {
    [button setFrame:CGRectMake( col * 80 + 5, row * 80 + 25, 70, 70)];

    if (col < 3) {
        col++;
    }
    else {
        row++;
        col = 0;
    }
}

}

However, cycling through subviews doesn't work when changing the order of the cells, since I'm pretty sure I'm not supposed to mess around in there at all. So, I wanted to cycle through my MatrixCells and use their order to get the appropriate view and then change the frame. However, the layout gets messed up at button 0 and I can't use them as buttons anymore.

- (void)layoutSubviews {

int row = 0;
int col = 0;
for (MatrixCell *cell in currentOrder) {
    UIView *view = [self viewWithTag:[cell order]];
    [view setFrame:CGRectMake( col * 80 + 5, row * 80 + 25, 70, 70 )];
    if (col < 3) {
        col++;
    }
    else {
        row++;
        col = 0;
    }
}

}

Here is a picture with the results: http://i52.tinypic.com/2q903lk.png

viewWithTag seemed the be the most elegant solution for this. So I'm at a loss. Any idea why these implementations don't render the same?

SpacyRicochet
  • 2,269
  • 2
  • 24
  • 39

2 Answers2

2

It looks to me rather like you have a background UIImage to provide that grey gradient backdrop. It also looks like your button 0 might have a [cell order] of 0. I'm going to guess that you've never changed the tag of your background UIImage, which will thus default to 0.

When you call [self viewWithTag] for the first UIButton (labeled 0), it will find the first subview with a tag 0 - the background image, not the UIButton you expected - and move that. This, I suspect. is why all the other buttons line up fine, why the background moves oddly, and why your 0 button is not visible.

Adam Wright
  • 48,938
  • 12
  • 131
  • 152
  • Not completely correct, but it brought me on the right track. I just set a `UIColor` for the background and the buttons just have a `setImage` as well. However, `[self viewWithTag:0]` returned `self`, of course completely messing up its frame, forgetting about button 0 and not allowing me to access the other buttons (since the frame didn't reach that far). But I solved it, so thanks! What's the protocol now, do I mark this answer as the correct one? Or should I answer my own question instead? – SpacyRicochet Jan 11 '11 at 18:01
  • Ah, my psychic debugging failed (but only just!). In this case, I'd probably write my own answer, and accept that. But an upvote never goes amiss ;) – Adam Wright Jan 11 '11 at 21:06
  • Alas, I'm still a lowly student without even enough reputation to vote. But when that becomes possible, I'll make sure to do so :) – SpacyRicochet Jan 11 '11 at 22:14
1

[self viewWithTag:0] returns self first, instead of the expected (UIButton *), since I didn't change the view's tag upon initialization. Apparantly tags default to 0 at initialization. layoutsubviews then proceeded to change the frame of self, messing up the layout and functionality. I solved this dilemma by changing the view's tag to 999 at initialization.

SpacyRicochet
  • 2,269
  • 2
  • 24
  • 39