UILabel没有回应触摸

时间:2011-05-27 23:57:05

标签: iphone

    label_win3=[[UILabel alloc] initWithFrame:CGRectMake(55, 358, 210,15)];
            label_win3.text=@"Click here";
            label_win3.textColor=[UIColor cyanColor];
            label_win3.backgroundColor=[UIColor clearColor];
            label_win3.font=[UIFont fontWithName:@"Helvetica" size: 14.0];
            label_win3.textAlignment=UITextAlignmentCenter;

            label_win3.userInteractionEnabled=YES;


        [self.view addSubview:label_win3];



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentTouch = [touch locationInView:self.view];


    if([touch view]==label_win3) {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"ShowRegistration" object:nil];}

     }

2 个答案:

答案 0 :(得分:2)

UILabels应该而且不能回应触摸。

TextViews,TextFields和Buttons都会响应触摸。

如果您不确定某个特定的UI元素是否可以进行用户交互,请阅读文档。

答案 1 :(得分:1)

使用userInteractionEnabled表示将触摸传递给视图。这并不意味着视图(UILabel)对它们有所帮助。 UILabel是一个非常轻量级的类。如果您想要回复UILabel上的触摸,请附加UIGestureRecognizer,对其进行子类化或使用自定义UIButton。很多时候,UIGestureRecognizer是最好的解决方案(尽管你应该尽可能使用UIButton;它包含许多重现的特殊行为)。

在任何情况下,您都没有理由期望调用touchesBegan:withEvent:。你没有被感动。触摸了UILabel,并且调用了 touchesBegan:withEvent:。然后,它对所设计的事件没有任何作用。

Jasarien也是正确的,你的问题非常不完整和令人困惑,但我认为我们已将它拼凑在一起。

相关问题