代码之家  ›  专栏  ›  技术社区  ›  Kevin Bomberry

uilabel:在标签上使用userInteractionEnabled方法

  •  9
  • Kevin Bomberry  · 技术社区  · 16 年前

    我想知道是否有人使用了uilabel上的userInteractionEnabled方法来允许标签像按钮一样工作(或者只是启动一个方法)。任何帮助都将不胜感激。干杯!

    更新(4/30/09@1:07pm)说明:我有一个标准的信息按钮,在它旁边我想放置一个带有文本“设置”的标签,我想让标签像按钮一样工作(它翻到设置屏幕上)。因此,基本上,我需要将已经定义的ShowSettinsView与“infoLabel”标签相关联;用户单击infoButton或infoLabel,方法就会启动。

    InfoButton已经在工作,正在使用IBAction来触发该方法。我想知道如何连接标签来实现相同的方法。仅此而已。干杯!

    3 回复  |  直到 13 年前
        1
  •  13
  •   Alex Reynolds    16 年前

    userInteractionEnabled 不是方法而是属性。但我想你会想把这个 YES 允许事件通过 UIView 超级视图。

    您可能要做的是重写 touchesBegan:withEvent: 方法 UIVIEW 包含您的 UIButton UILabel 子视图。

    在此方法中,测试 UITouch 触摸落在 乌拉贝尔 .

    也就是说,是不是 CGPoint 要素 [touch locationInView] 与…相交 CGRect 要素 [infoLabel bounds] ?查看函数 CGRectContainsPoint 运行此测试。

    如果是这样,那么就启动一个 NSNotification 也一样。 IBAction 选择器作为 乌布顿 .

        2
  •  7
  •   MonsieurDart    15 年前

    另一种解决方案是使用 UIButton 其类型设置为自定义,而不是 UILabel . 这样,您的第二个按钮将看起来像 乌拉贝尔 ,您可以将其配置为触发 showSettingsView 方法。

        3
  •  2
  •   sunilg    13 年前

    我同意亚历克斯2009年4月30日的观点,如果你不想使用uibutton,我的建议只是一个补充。我解决这个问题的方法是通过

    - (id)initWithFrame:(CGRect)frame
    {
        label = [[UILabel alloc] initWithFrame: CGRectMake(5, 5, 20, 20)];
        ...
        [label setUserInteractionEnabled: YES];
        [self addSubview: label];
    }
    

    然后,您可以重写touchesbegan等touchesbegan方法,并使用类似的

    - (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
          UITouch *touch = [touches anyObject];
          if([touch view] == label) {
              NSLog(@"I've been touched");
           }
       }