0

I'm working on the Minesweeper game, I want to add the flag when user long tap on a tile of the gameboard. I've implemented this code:

For every button in gameboard:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressTap:)];
            longPress.minimumPressDuration = 1.0f;
            [self.button addGestureRecognizer:longPress];

In self, the method longPressTap:

- (void)longPressTap:(Tile *)sender {
        if (sender.block.marking ==  MARKING_FLAGGED) {
            // if already a flag I mark as a blank tile, with color defined for gameboard
            sender.backgroundColor = UIColorFromRGB(0x067AB5);
            sender.block.marking = MARKING_BLANK;
            self.flagCount++;
        }
        else{
            // if it's not a flag I mark as a flag and set the flag image for the tile
            [sender setBackgroundImage:[UIImage imageNamed:IMAGE_NAME_FLAG] forState:UIControlStateNormal];
            sender.block.marking = MARKING_FLAGGED;
            self.flagCount--;
        }
}

And of course self is my UIGestureRecognizerDelegate. But when I try to long press on a tile, the app crash and give this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILongPressGestureRecognizer block]: unrecognized selector sent to instance 0x8cf2b00'

What should I do? I'm at the very beginning with Obj-C programming so if someone can help me and explain what I did wrong I'll be very grateful.

Gio Bact
  • 541
  • 1
  • 7
  • 23

3 Answers3

2
- (void)showOptions:(UILongPressGestureRecognizer*)sender{

UIButton *btn = (UIButton*)sender.view;
NSLog(@"view tag %d",sender.view.tag);

if (sender.state == UIGestureRecognizerStateEnded)
{

}
else if (sender.state == UIGestureRecognizerStateBegan)
{
   [self.bubbleDelegate showOptionsForMessage:btn];
}

}

Waseem Sarwar
  • 2,645
  • 1
  • 21
  • 18
0

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILongPressGestureRecognizer block]: unrecognized selector sent to instance 0x8cf2b00'

From this error it is very clear app is crashing because of [UILongPressGestureRecognizer block]. UILongPressGestureRecognizer doesnt have a method called block so it is crashing.

- (void)longPressTap:(Tile *)sender {
}

As you expected in the implementation sender in this method is not Tile object, it is actually UILongPressGestureRecognizer.

The expected method is

- (void)longPressTap:(UILongPressGestureRecognizer *)sender
{
}
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • Yeah this is exactly what I thought was wrong, but passing UILongPressGestureRecognizer as parameter for longPressTap: how can I check what Tile user choose and mark it as a flag/blank? Sorry for noob questions but I'm trying to understand – Gio Bact Jun 18 '14 at 09:01
  • use `[sender locationInView]` to find the location of touch from there you should have a logic to get the associated `Tile` – Anil Varghese Jun 18 '14 at 09:05
0

I understand your problem . Can I now what is "Tile" in argument.

- (void)longPressTap:(Tile *)sender

use this may be helpfull

- (void)longPressTap:(UILongPressGestureRecognizer *)sender

and don't use direct Tile. Use Tile object direct here and make global object of this ..

Viper
  • 1,408
  • 9
  • 13
  • I was passing Tile as a parameter because I didn't know how to identify the Tile selected by the user. So now I have: - (void)longPressTap:(UILongPressGestureRecognizer *)sender But can you please give me a snippet for marking the tile as flag/blank? ty – Gio Bact Jun 18 '14 at 09:09
  • try adding long tap gesture to Tile object instead of button, and use Tile object as sender argument, may it will work – Viper Jun 18 '14 at 09:17