JZ.
JZ.

Reputation: 21877

How to assign a gesture recognizer to a collection?

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?

Upvotes: 0

Views: 61

Answers (1)

Jeffery Thomas
Jeffery Thomas

Reputation: 42598

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.

Upvotes: 1

Related Questions