0

By what method possible can I discover the text from a UILabel using the touchesBegan event?

The current code so far is...

- (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);

     }

I wish I could simply write these... but these are all nefarious for different devious reasons

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);

This is almost functionally equivalent code, but the general functionality is now dependent on 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;
      }
  }

Help with this coding problem would be great

4

2 回答 2

0

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

于 2012-05-17T20:39:07.583 回答
0
UILabel * label = [[UILabel alloc] init];
UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped:)];
[label addGestureRecognizer:tapGestureRecognizer];

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

于 2012-05-23T09:29:06.580 回答