1

Currently I have a button that makes a draggable UIView with a subview of a UIButton. When I long press that UIButton, an alert view comes up and I have two buttons, a delete button and a cancel button. The delete button is supposed to delete the last long pressed UIButton, however it deletes the most recently made UIButton.

I would like for the delete button on the alert view to delete the last long pressed UIButton.(not the most recently created) I have tried different if statements, but this is what I have so far. Here is my code for my .m file:

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateBegan ) {

    UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:@"Would you like to delete this rep?"
                                  message:nil
                                  preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* deleteButton = [UIAlertAction
                                actionWithTitle:@"Delete"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action)
                                {



                                        [_buttonField removeFromSuperview];

                                    [alert dismissViewControllerAnimated:YES completion:nil];

                                }];
    UIAlertAction* cancelButton = [UIAlertAction
                               actionWithTitle:@"Cancel"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action)
                               {
                                   [alert dismissViewControllerAnimated:YES completion:nil];

                               }];

    [alert addAction:deleteButton];
    [alert addAction:cancelButton];

    [self presentViewController:alert animated:YES completion:nil];
    }
    }

    - (void)panWasRecognized:(UIPanGestureRecognizer *)panner {

    {
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.buttonField addGestureRecognizer:longPress];

     _draggedView = panner.view;

    CGPoint offset = [panner translationInView:_draggedView.superview];
    CGPoint center = _draggedView.center;
    _draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);
    _draggedView.layer.borderWidth = 2.0f;
    _buttonField.layer.borderColor = [UIColor blackColor].CGColor;
    [_buttonField setTintColor:[UIColor magentaColor]];





    // Reset translation to zero so on the next `panWasRecognized:` message, the
    // translation will just be the additional movement of the touch since now.
    [panner setTranslation:CGPointZero inView:_draggedView.superview];

    }

    }



    - (IBAction)addRepButton:(UIBarButtonItem *)newRep {

    self.labelCounter++;

    buttonCount ++;
    if (buttonCount >= 0 )
    {

    _buttonField = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 28, 28)];
    [_buttonField setTitle:[NSString stringWithFormat:@"%i", self.labelCounter]forState:UIControlStateNormal];
    [_buttonField setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _buttonField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    _buttonField.userInteractionEnabled = YES;
    _buttonField.layer.cornerRadius = 14;
    _buttonField.layer.borderColor = [UIColor blackColor].CGColor;
    _buttonField.layer.borderWidth = 2.0f;
    _buttonField.titleLabel.font = [UIFont systemFontOfSize: 18];



    UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc]
                                      initWithTarget:self action:@selector(panWasRecognized:)];



    [_buttonField addGestureRecognizer:panner];


    [self.view addSubview:_buttonField];


    }


    }

How do I go about making the delete button remove the most recently long pressed _buttonField?

princ___y
  • 1,089
  • 1
  • 9
  • 27
Lozo
  • 79
  • 10

2 Answers2

2

You are saying:

[_buttonField removeFromSuperview];

Well, as your loop shows (inside addRepButton), _buttonField is the most recently added button, because every time you add a button, you set it to that button. So what is happening is exactly what you are saying to happen.

I presume, although it is a little hard to tell from your code, that the button you want to delete is the one whose long press gesture recognizer this is — that is, gesture.view.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • right, so how do I go about setting it to the most recently touched up inside, long pressed, etc? – Lozo Oct 30 '15 at 04:43
  • I don't even understand what you think `_buttonField` is good for. I don't know why you are bothering to keep setting it to button after button after button. As for your specific question, I believe I've answered it very clearly in my answer above. I've actually told you the code that refers to the button that was pressed. – matt Oct 30 '15 at 04:45
  • I agree you have done that, but I am just not sure how to make the last long pressed as the one "selected" – Lozo Oct 30 '15 at 04:51
  • Really? So changing `[_buttonField removeFromSuperview];` to `[gesture.view removeFromSuperview]` doesn't do what you're after? – matt Oct 30 '15 at 04:54
  • you wizard you... thank you for the tip, I'm relatively new to coding and I am glad that you helped me out. thank you again – Lozo Oct 30 '15 at 05:03
0
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
  if ( gesture.state == UIGestureRecognizerStateBegan ) {

     //Update
      UIButton *buttonPressedLatest;
      UIView *ifBtnPressed = gesture.view;
       if([ifBtnPressed isKindOfClass:[UIButton class]]){
          buttonPressedLatest = (UIButton *)ifBtnPressed;
        }

UIAlertController * alert=   [UIAlertController
                              alertControllerWithTitle:@"Would you like to delete this rep?"
                              message:nil
                              preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* deleteButton = [UIAlertAction
                            actionWithTitle:@"Delete"
                            style:UIAlertActionStyleDefault
                            handler:^(UIAlertAction * action)
                            {
   [buttonPressedLatest removeFromSuperview];

                                [alert dismissViewControllerAnimated:YES completion:nil];

                            }];
UIAlertAction* cancelButton = [UIAlertAction
                           actionWithTitle:@"Cancel"
                           style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction * action)
                           {
                               [alert dismissViewControllerAnimated:YES completion:nil];

                           }];

[alert addAction:deleteButton];
[alert addAction:cancelButton];

[self presentViewController:alert animated:YES completion:nil];
}
}

Try this once and tell me if this works.

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
  • I apologize, but I did not try this because someone has already answered, thanks for the input, i will keep you updated if i ever try it – Lozo Oct 30 '15 at 05:04