1

目前,我有一个按钮,可以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?

4

2 回答 2

2

你是说:

[_buttonField removeFromSuperview];

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

我认为,虽然从您的代码中很难判断,但您要删除的按钮是其长按手势识别器的按钮,即gesture.view.

于 2015-10-30T04:41:53.847 回答
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];
}
}

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

于 2015-10-30T04:58:18.333 回答