来自touchesbegan事件的UILabel文本

时间:2012-05-17 18:55:08

标签: objective-c uiview uilabel touchesbegan

使用touchesBegan事件,我可以通过什么方法从UILabel中发现文本?

目前的代码是......

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

     UITouch *touch = [touches anyObject];
     CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
     UIView* viewYouWishToObtain = [self.view hitTest:locationPoint withEvent:event];

     ////
     //insert code here to get currentLabel - not using viewYouWishToObtain.tag
     ////

     NSLog(@"text: %@", currentLabel.text);

     }

我希望我能简单地写下这些......但是由于不同的原因,这些都是邪恶的

UILabel* currentLabel = [self.view hitTest:locationPoint withEvent:event];
NSLog(@"text: %@", currentLabel.text);

NSLog(@"text: %@",[self.view hitTest:locationPoint withEvent:event].text);

UILabel *newlabel = (UILabel*) event;
NSLog(@"text: %@", newlabel.text);

这几乎是功能相同的代码,但一般功能现在依赖于NSDictionary。

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray* myword=[NSArray arrayWithObjects:@"h",@"e",@"l",@"l",@"o",nil];

    NSDictionary *letterToNumber;
    letterToNumber = [NSDictionary dictionaryWithObjectsAndKeys:
                @"0", @"a", 
                @"1", @"b", 
                @"2", @"c", 
                @"3", @"d",
                @"4", @"e", 
                @"5", @"f", 
                @"6", @"g", 
                @"7", @"h", 
                @"8", @"i", 
                @"9", @"j", 
                @"10", @"k", 
                @"11", @"l", 
                @"12", @"m", 
                @"13", @"n", 
                @"14", @"o", 
                @"15", @"p", 
                @"16", @"q", 
                @"17", @"r", 
                @"18", @"s", 
                @"19", @"t", 
                @"20", @"u", 
                @"21", @"v", 
                @"22", @"w", 
                @"23", @"x", 
                @"24", @"y", 
                @"25", @"z", 
                nil];    

    NSUInteger characterCount = [myword count];

    for (int i=0;i<characterCount ;i++){
    UILabel*myLabel;
    myLabel=[[UILabel alloc] initWithFrame: CGRectMake((1+i)*35.0, 100.0, 30.0, 30.0)];
    myLabel.backgroundColor = [UIColor whiteColor];
    myLabel.text= [myword  objectAtIndex:i];
    myLabel.userInteractionEnabled = YES;
    myLabel.tag=100+[[letterToNumber objectForKey:[myword objectAtIndex:i]] integerValue];        
    [self.view addSubview:myLabel];     
    }
 }

   - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
      UITouch *touch = [touches anyObject];
      NSLog(@"1");

      CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
      UIView* viewYouWishToObtain = [self.view hitTest:locationPoint withEvent:event];

      NSArray*myarray;
      myarray = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",nil];

      NSUInteger temp = viewYouWishToObtain.tag;
      if (temp >= 100){
      NSLog(@"text: %@",[myarray  objectAtIndex:temp-100]);
      }

      if ([touch view] != viewYouWishToObtain && (viewYouWishToObtain.tag >= 100)) {
          if ([touch tapCount] == 2) {
          }
          return;
      }
  }

帮助解决这个编码问题会很棒

2 个答案:

答案 0 :(得分:0)

出于好奇,您是否可以从使用UILabel切换到仅使用自定义类型的UIButton。它应该看起来就像UILabel那样,您可以正常连接触摸事件而不需要命中。我认为这会让事情变得更容易。

答案 1 :(得分:0)

UILabel * label = [[UILabel alloc] init];
UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped:)];
[label addGestureRecognizer:tapGestureRecognizer];

然后使用labelTapped函数追溯标签并获取文本;)