0

My goal is to simply send the correct button.titleLabel.text to the handleLongPress function of my view controller. This is my function:

- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)sender {
    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = 1.0;
    [self setUserIntendsToChangeUIsoTheUIisLockedUntilUserSelection:YES];
    NSLog(@"sender? %@", sender);
    if ([sender.view isKindOfClass:[UIButton class]]) {
        self.myButton = (UIButton *)sender.view;
        NSLog(@"!!!!! %@", self.myButton.titleLabel.text);
        [self.view addSubview:self.scrollPickerView];     
    }

}

This is my storyboard of which I have created a referencing outlet collection of buttons "H", "Cl", "C" etc etc.

enter image description here

Each button does respond to the UILongPressGesture, however the logged message NSLog(@"!!!!! %@", self.myButton.titleLabel.text); always references the same UIButton "C", even if I hold a different button. What have I done wrong? How can I get each button to send its title to the handleLongPress function?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
JZ.
  • 21,147
  • 32
  • 115
  • 192

1 Answers1

1

I've run into this before. In Interface Builder, you only have 1 UILongPressGestureRecognizer. You need a separate UILongPressGestureRecognizers for each view. What you should see in Interface Builder is a long row of UILongPressGestureRecognizer icons on the bottom bar.

As a side note:

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = 1.0;

is wasted code. You create a new variable and do nothing with it.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117