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

标签 ios objective-c uibutton uialertview uilongpressgesturerecogni

目前我有一个按钮,它使 UIView 具有 UIButton 的 subview 。当我长按 UIButton 时,会出现一个警报 View ,我有两个按钮,一个是删除按钮,一个是取消按钮。删除按钮应该删除最后一次长按的 UIButton但是它会删除最近制作的 UIButton

我希望警报 View 上的删除按钮删除最后一次长按的 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

关于ios - 触发操作时,以编程方式制作的 UIButton 不会删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33428729/

相关文章:

ios - 将数据传递到搜索栏

ios - Objective-C 修改类实例父类(super class)

iphone - 在 Objective C 中阅读电子邮件(例如 pop3)

objective-c - NSString 中 %@ %d 的名称

ios - 如何使表格 View 在快速重新加载数据后显示特定行?

ios - 对象发送-自动释放次数过多(iOS5)

objective-c - 什么是现代运行时?

ios - 知道为什么自定义导航栏后退按钮图像没有使用此代码显示吗?

ios - 如何管理被禁用/启用的按钮

objective-c - UIButton 在 UIScrollView 中不起作用