【问题标题】:How to get button.tag via longPressGestureRecognizer?如何通过 longPressGestureRecognizer 获取 button.tag?
【发布时间】:2023-03-29 23:17:01
【问题描述】:

我正在向某些滚动视图动态添加图像按钮。它们都指向一个 longPressHandler。现在,我如何知道按下了哪个按钮? [sender tag] 为我提供了我添加到按钮的 longGestureRecognizer 的标签,我无法手动设置该标签。

for (...) {
    UIButton *button = [[UIButton alloc] init];
    button.tag = w + h * 3; 
    [button addTarget:self action:@selector(imageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UILongPressGestureRecognizer *gest = [[UILongPressGestureRecognizer alloc]
                initWithTarget:self action:@selector(imageButtonLongPress:)];
    gest.minimumPressDuration = 1;
    gest.delegate = self;

    [button addGestureRecognizer:gest];
    [gest release];

    [scrollView addSubview:button];
    [button release];
}

- (void) imageButtonLongPress:(id)sender {  
    // how to get button tag here?
}

【问题讨论】:

    标签: iphone cocoa-touch button tags sender


    【解决方案1】:

    UIGestureRecognizer 中有一个view 属性,它返回识别器附加到的视图。我认为这是你最好的选择。

    - (void) imageButtonLongPress:(id)sender {  
        UIGestureRecognizer *recognizer = (UIGestureRecognizer*) sender;
        int tag = recognizer.view.tag;
    }
    

    【讨论】:

    • 这太棒了。谢谢
    • recognizer.view.tag 给了我点击 UIButton 的错误标签。有什么解决办法吗?
    【解决方案2】:

    在您的操作中,您必须以手势输入发送者,然后输入将其视图转换为按钮,然后将按钮的标签设为 -

    UILongPressGestureRecognizer *gest = (UILongPressGestureRecognizer *)sender;
    UIButton *button = (UIButton*)[gest view];
    NSLog(@"%d",[button tag]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 2011-02-03
      • 2011-04-02
      • 2019-07-20
      • 2013-06-17
      • 2017-04-08
      相关资源
      最近更新 更多