这是一个奇怪的问题。
我对 Xcode 很陌生。我一直在尝试制作一个简单的应用程序,当您单击按钮视图时添加新按钮并在双击时删除它们。
添加按钮是可以的,但删除它们是不可靠的。我认为这与我编写代码的方式有关,因为它似乎只删除了双击时最近添加的按钮,而不是我单击的实际按钮。
我的删节 .m 代码如下:
- (void)viewDidLoad
{
- (void)handleSingleTap:(UITapGestureRecognizer *)tapper {
//adds the buttons and gives them a unique tag
ButtonCount = ButtonCount+1;
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 150, 150);
btn.userInteractionEnabled = YES;
btn.tag=PuckCount;
//attaches double tap recognizer to button
UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleDoubleTap:)];
[doubleTapGestureRecognizer setNumberOfTapsRequired:2];
[btn addGestureRecognizer:doubleTapGestureRecognizer];
//Add Tap Recognizer to pucks to create new buttons
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleSingleTap:)];
[singleTapGestureRecognizer setNumberOfTapsRequired:1];
[singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGestureRecognizer];
[btn addGestureRecognizer:singleTapGestureRecognizer];
UIImage *buttonImage = [UIImage imageNamed:@"puck2.png"];
[btn setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:btn];
}
//handles what happens on a double tap - THIS IS WHERE I THINK THE PROBLEM IS.
- (void)handleDoubleTap:(UIGestureRecognizer *)doubletap{
PuckSelected = self.view.tag;
[[self.btn viewWithTag:PuckSelected] removeFromSuperview];
}
@end