gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:23:53 25 4
gpt4 key购买 nike

目前我有一个按钮,它使 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com