触发操作时,以编程方式制作的 UIButton 不会删除

Programmatically made UIButton is not deleting when action fired

目前我有一个按钮,可以使 UIView 具有 UIButton 的子视图。当我长按 UIButton 时,会出现一个警告视图,我有两个按钮,一个删除按钮和一个取消按钮。删除按钮应该删除最后一次长按 UIButton,但它会删除最近按下的 UIButton

我希望警报视图上的删除按钮删除最后一次长按 UIButton。(不是最近创建的)我尝试了不同的 if 语句,但这是我目前所拥有的.这是我的 .m 文件的代码:

- (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];


    }


    }

如何让删除按钮删除最近长按的 _buttonField?

您是说:

[_buttonField removeFromSuperview];

好吧,正如您的循环所示(在 addRepButton 内),_buttonField 最近添加的按钮,因为每次添加按钮时,您将其设置为该按钮。所以正在发生的事情正是你所说的发生的事情。

我推测,虽然从您的代码中很难分辨出您要删除的按钮是其长按手势识别器的按钮,即 gesture.view.

- (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];
}
}

试试这个,然后告诉我它是否有效。