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];}
}
UILabels should and cannot respond to touch.
TextViews, TextFields, and Buttons all respond to touch.
Read the documentation if you are uncertain whether a particular UI element can have user interaction.
Having userInteractionEnabled
means that touches are passed to the view. It does not mean that the view (UILabel
) does something with them. UILabel is a very lightweight class. If you want to respond to touches on a UILabel, attach a UIGestureRecognizer
to it, subclass it or use a custom UIButton
. Many times, UIGestureRecognizer
is the best solution for this (though you should use a UIButton
whenever you can; it includes many specialized behaviors that are a pain to reproduce).
In any case, there is no reason for you to expect that your touchesBegan:withEvent:
would be called. You were not touched. The UILabel was touched, and its touchesBegan:withEvent:
was called. It then did nothing with that event as designed.
Jasarien is also correct that your question is very incomplete and confusing, but I think we've pieced it together.
精彩评论