Is there a touch method for UILabel?

后端 未结 5 534
不思量自难忘°
不思量自难忘° 2020-12-28 17:41

I\'d like to do an action if someone touches a predeclared UILabel, something like:

if (label is touched) {
    my actions;
}

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-28 18:31

    You could use a gesture recognizer:

    - (void)someSetupMethod {
        // ...
        label.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = \
        [[UITapGestureRecognizer alloc]
         initWithTarget:self action:@selector(didTapLabelWithGesture:)];
        [label addGestureRecognizer:tapGesture];
        [tapGesture release];
    }
    
    - (void)didTapLabelWithGesture:(UITapGestureRecognizer *)tapGesture {
        // ...
    }
    

提交回复
热议问题