2012-05-17 88 views
0

通過什麼方法可以使用touchesBegan事件從UILabel中發現文本?來自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; 
     } 
    } 

幫助這個編碼問題將是巨大的

+0

哪個標籤?如何挑選哪個標籤的文字? – Alexander

+0

如果在視圖中碰到任何標籤,我想獲取文本,但是我不能使用.tag – MGM

回答

0

只是出於好奇,你能也許使用UILabel的,只是使用的UIButton與自定義類型轉換。它應該看起來就像UILabel那樣,你可以正常連接觸摸事件,而不需要碰觸測試。我認爲這會讓事情變得更容易。

+0

我希望我可以使用UIButton,但是您必須使用passhandlers,這會在用戶界面touchesMoved事件。 – MGM

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

然後使用labelTapped功能追溯標籤和得到的文本;)