2011-12-23 18 views
9

Ho creato alcune serie di etichette a livello di codice da visualizzare sullo schermo e dopo aver fatto clic sull'etichetta, è necessario eseguire un'azione.Azione su UILabel in iphone

Si prega di non suggerirmi su UIButton. Voglio farlo per UILabel. Dopo aver fatto clic sull'etichetta, dovrebbe apparire un'altra vista di dettaglio.

Please help me per risolvere questo problema senza utilizzare Inteface Builder.

+1

Cosa c'è che non va con UIButton? Penso che un UIbutton trasparente non abbia nulla di diverso con quello che vuoi. – Skyler

risposta

22

Infine mi è venuta con la soluzione e ho ottenuto il risultato

[titleLbl setUserInteractionEnabled:YES]; 
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelButton:)]; 
    [tapGestureRecognizer setNumberOfTapsRequired:1]; 
    [titleLbl addGestureRecognizer:tapGestureRecognizer]; 
    [tapGestureRecognizer release]; 
4

rendere IBoutlet dell'etichetta, implementare touchesBegin nel controller, estrarre il CGPoint - touchCoordinate, controllare

CGRectContainsPoint(label.frame,touchCoordinate) 
{ 
//you got the touch action on your label 
} 
+1

pls dimmi che senza IB –

+1

imposta il tag dell'etichetta mentre lo crea: supponi di aver impostato il tag: 199. quindi, puoi recuperare l'etichetta quando richiesto da UILabel * label = [self.view viewWithTag: 199]; – samfisher

+1

puoi recuperare l'oggetto dell'etichetta senza IBOutlet usando sopra. Spero che possa essere d'aiuto .. – samfisher

3

In

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
....... 
....... 
CGPoint touch;//Touch Location 

if(CGRectContainsPoint([objectOfLable frame], [touch locationInView:self.view ])) 
{ 
    Do What you Want 
} 

} 

Try This