Long click gesture recognizer on UIButton?

I am working on a Minesweeper game, I want to add a flag when the user long presses on the tile of the playing field. I have implemented this code:

For each button in the playing field:

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

In self, the longPressTap method:

 - (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 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 I'm my UIGestureRecognizerDelegate. But when I try to press the tile for a long time, the application crashes and throws this error:

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

What should I do? I am at the very beginning of Obj-C programming, so if someone can help me and explain what I did wrong, I will be very grateful.

0
source share
3 answers
 - (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]; } 

}

+2
source

* Application termination due to an uncaught exception "NSInvalidArgumentException", reason: '- [UILongPressGestureRecognizer block]: unrecognized selector sent to instance 0x8cf2b00'

Due to this error, a very explicit application crashes due to the [UILongPressGestureRecognizer block] . UILongPressGestureRecognizer does not have a method called block , so it fails.

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

As expected in the sender implementation, there is no Tile object in this method, it is actually a UILongPressGestureRecognizer .

Expected Method

 - (void)longPressTap:(UILongPressGestureRecognizer *)sender { } 
0
source

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

 - (void)longPressTap:(Tile *)sender 

use it may be useful

 - (void)longPressTap:(UILongPressGestureRecognizer *)sender 

and do not use direct tile. Use the Tile object right here and create a global object of it.

0
source

Source: https://habr.com/ru/post/972253/


All Articles