I’ve just started programming and I’ve got this far but I’ve been struggling with this now for a while and am completely stuck. I’ve created a 10×10 board of playing cards, from two decks. I’ve then made them all buttons in -(void)displayBoard below. Each card has a BOOL property card.button holding whether it should be enabled as a button or not.
I’m switching them on OK and all works fine. They start off untouchable, then I switch them on and they correctly call (void)boardCardPressed. However, when I switch them off there they graphically appear to be off, but are still calling (void)boardCardPressed when I touch them. If I log the button property, that is being set correctly. I suspect it’s something to do with me adding the target and not removing, but everything i try makes no difference.
I’ve not found any help on this anywhere so any ideas, tips or help would be very much appreciated. BTW – I suspect my attempts aren’t the most elegant, so tips there would be great too. It was the only way I could find to get my card images to be selectable 🙂
-(void)displayBoard{
board = [model board];
for (int x = 0; x < 10; x++){
for(int y = 0; y < 10; y++){
Card *tCard = [board getCardxpos:x ypos:y];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = tCard.tag;
button.enabled = tCard.button;
button.adjustsImageWhenDisabled = NO;
[button setImage:tCard.image forState:UIControlStateNormal];
button.frame = CGRectMake(tCard.xPos, tCard.yPos, tCard.sizeW, tCard.sizeH);
[button setImage:tCard.image forState:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(boardCardPressed:) forControlEvents:UIControlEventTouchUpInside];
[(SequenceView *)self.view drawIt:button];
}
}
}
-(void)boardCardPressed:(id)sender {
int tid = ((UIControl*)sender).tag;
int x = tid / 10;
int y = tid%10;
Card *tCard = [[model board] getCardxpos:x ypos:y];
tCard.chosen = YES;
tCard.button = NO;
[self displayBoard];
}
There is something that is not entirely clear to me in your code. As far as I see, you create the displayBoard and includes 100 buttons in it; when any of those buttons is touched, the
boardCardPressedis called again and you create again the board, with its 100 buttons that have nothing to do with the original ones.Is this meant to be so? Or could this have to do with the behavior you don’t understand?