0

我的目标是简单地将正确的发送button.titleLabel.text到我的视图控制器的 handleLongPress 函数。这是我的功能:

- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)sender {
    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = 1.0;
    [self setUserIntendsToChangeUIsoTheUIisLockedUntilUserSelection:YES];
    NSLog(@"sender? %@", sender);
    if ([sender.view isKindOfClass:[UIButton class]]) {
        self.myButton = (UIButton *)sender.view;
        NSLog(@"!!!!! %@", self.myButton.titleLabel.text);
        [self.view addSubview:self.scrollPickerView];     
    }

}

这是我的故事板,我创建了一个引用出口集合的按钮“H”、“Cl”、“C”等。

在此处输入图像描述

每个按钮都会响应UILongPressGesture,但记录的消息NSLog(@"!!!!! %@", self.myButton.titleLabel.text);始终引用相同的 UIButton“C”,即使我按住不同的按钮也是如此。我做错了什么?如何让每个按钮将其标题发送到 handleLongPress 函数?

4

1 回答 1

1

我以前遇到过这个。在 Interface Builder 中,您只有 1 UILongPressGestureRecognizer。每个视图都需要一个单独UILongPressGestureRecognizer的 s 。您应该在 Interface Builder 中看到的是UILongPressGestureRecognizer底栏上的一长排图标。

作为旁注:

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = 1.0;

是浪费代码。您创建了一个新变量,但什么也不做。

于 2014-08-17T21:02:25.660 回答