Manejo de eventos táctiles en UILabel y conectarlo a una IBAction


Ok, así que tengo un UILabel creado en interface builder que muestra algunos algunos texto predeterminado de "tap to begin".

Cuando el usuario toca el UILabel quiero que active un método IBAction: -(IBAction)next; que actualiza el texto de la etiqueta para decir algo nuevo.
Sería muy conveniente si esto me permitiera simplemente arrastrar una conexión desde mi método a mi etiqueta y luego seleccionar retocar dentro, como con un botón. pero, por desgracia, ningún cigarro.

Así que de todos modos, supongo que mi pregunta es: ¿vas a tener que subclase UILabel para que esto funcione? O hay alguna manera de que pueda arrastrar un botón sobre la etiqueta, pero hacerlo 0% opaco. ¿O hay una solución más simple que me falta?

Author: Mihir Oza, 2010-07-03

4 answers

Compruébalo:

UILabel *label = ...
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
      [[UITapGestureRecognizer alloc] initWithTarget:self 
                                              action:@selector(labelTap)];
[label addGestureRecognizer:tapGesture];

El truco es habilitar la interacción del usuario.

 253
Author: Scott Persinger,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-15 19:35:28

UILabel hereda de UIView que hereda de UIResponder. Todos los objetos UIResponder pueden manejar eventos táctiles. Así que en su archivo de clase que conoce sobre su vista (que contiene la etiqueta UILabel) implementar:

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

En interface builder establece el valor de la etiqueta UILabel. cuando se produzcan toques en el método touchesBegan, compruebe el valor de la etiqueta de la vista a la que pertenece la etiqueta:

UITouch *touch = [touches anyObject];

if(touch.view.tag == MY_TAG_VAL)
label.text = @"new text";

Conecta su código en su archivo de clase con el objeto UILabel en interface builder mediante declarar la variable de instancia UILabel con el prefijo IBOutlet:

IBOutlet UILabel *label;

Luego en interface builder puedes conectarlos.

 70
Author: Remover,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-23 02:11:50

Puede usar un UIButton, hacerlo transparente, es decir, tipo personalizado sin una imagen, y agregar una etiqueta UILabel (centrada). Luego conecte los eventos normales del botón.

 30
Author: Eiko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-03 00:48:42

Swift 3

Tienes un IBOutlet

@IBOutlet var label: UILabel!

En el que habilita la interacción del usuario y agrega un reconocedor de gestos

label.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(userDidTapLabel(tapGestureRecognizer:)))
label.addGestureRecognizer(tapGesture)

Y finalmente, manejar el grifo

func userDidTapLabel(tapGestureRecognizer: UITapGestureRecognizer) {
  // Your code goes here
}
 10
Author: César Cruz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-13 14:15:47