sma*_*nja 2 iphone objective-c ios4 ios
我有三个 UILabel。我想检测哪个标签被点击,然后检索该标签的字符串值。这就是我正在尝试的方式,我只能设法检测被点击的位置,但我无法检测被点击的标签。
标签创建
for (NSInteger i=1; i<=[pdfs count]; i++){
UILabel *newLabel=[[UILabel alloc] init];
newLabel.text = [NSString stringWithFormat:[[pdfs objectAtIndex:(i-1)] lastPathComponent]];
newLabel.frame = CGRectMake(10, 60*i, 320, 20);
newLabel.tag=i;
newLabel.font = [UIFont systemFontOfSize:20.0f];
newLabel.backgroundColor = [UIColor clearColor];
newLabel.userInteractionEnabled = YES;
[self.view addSubview:newLabel];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[newLabel addGestureRecognizer:singleTap];
[newLabel release], newLabel=nil;
[singleTap release];
}
Run Code Online (Sandbox Code Playgroud)
检测水龙头
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
Run Code Online (Sandbox Code Playgroud)
{
CGPoint location;
location = [recognizer locationInView:self.view];
NSString *documentName;
if(location.y<150.0){
documentName = [[pdfs objectAtIndex:0] lastPathComponent];
}
else{
documentName = [[pdfs objectAtIndex:1] lastPathComponent];
}
Run Code Online (Sandbox Code Playgroud)
GestureRecognizer正如您在标签上添加的那样
// called when touch is began or when user touches
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
UILabel *theLabel = (UILabel *)touch.view;
if (theLabel.tag == 1)
{}
else if ...
}
Run Code Online (Sandbox Code Playgroud)