1

UIView現在、のサブビューでドラッグ可能にするボタンがありますUIButton。それを長押しするUIButtonと、アラート ビューが表示され、削除ボタンとキャンセル ボタンの 2 つのボタンが表示されます。削除ボタンは、最後に長押しされたものを削除するはずです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 に答える