【问题标题】:How to assign a gesture recognizer to a collection?如何将手势识别器分配给集合?
【发布时间】:2014-08-17 18:19:14
【问题描述】:

我的目标是简单地将正确的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 函数?

【问题讨论】:

    标签: objective-c ios7 uigesturerecognizer iboutletcollection


    【解决方案1】:

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

    附注:

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

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

    【讨论】:

      猜你喜欢
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      相关资源
      最近更新 更多