2012-03-16 39 views
7

Estoy creando botones programáticamente. Quiero cambiar el color de fondo mientras se toca el botón en el interior de nuevo tiene que establecer de nuevo a su color habitual después de levantar el dedo .....Cambiar el color de fondo UIButton

nine = [UIButton buttonWithType:UIButtonTypeCustom]; 
[nine setFrame:CGRectMake(15, 105, 65, 40)]; 
[nine setTitle:@"9" forState:UIControlStateNormal]; 
[nine setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
[nine setBackgroundColor:[UIColor cyanColor]]; 
[nine addTarget:self action:@selector(clickDigit:) forControlEvents:UIControlEventTouchUpInside]; 
[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside]; 

[self.view addSubview:nine]; 

// para cambiar el color de fondo

-(void)changeButtonBackGroundColor:(id) sender 
{ 
    [nine setBackgroundColor:[UIColor redColor]]; 
} 

Aquí se creó el método changeBackgroundColor para cambiar el color de ese botón. cambia de color

+0

"Touch Up" es igual a levantar el dedo. – dasdom

+1

Entonces ... ¿cuál es la pregunta exacta? ¿Escribiste métodos para las acciones que registraste? Si es así, podrías agregar ese código. – DBD

+0

Marca esta publicación https://somethingaboutios.wordpress.com/2016/02/09/uibutton-backgroundcolor-for-uicontrolstateselected/ –

Respuesta

14

No sé si esto se relaciona con su pregunta, pero: este

[nine setBackgroundColor:[UIColor redColor]]; 

debe ser

[sender setBackgroundColor:[UIColor redColor]]; 

Editar: cambiar esta

[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside]; 

a

[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchDown]; 
[nine addTarget:self action:@selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside]; 
[nine addTarget:self action:@selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpOutside]; 
[nine addTarget:self action:@selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchCancel]; 

y añadir el método:

- (void)resetButtonBackGroundColor: (UIButton*)sender { 
    [sender setBackgroundColor:[UIColor cyanColor]]; 
} 
+0

Este método hace que cambie el color pero no vuelve al color cian después de levantar el dedo – Karthikeya

+0

Ambos trabajos el mismo – Karthikeya

+0

Editado mi respuesta. – dasdom

2

yo creo que hay dos opciones ..

Primero:

Usted puede poner el setBackgroundColor [nueve: [UIColor redcolor] ]; dentro "clickDigit" (y al igual que dasdom dijo a cambiar el nombre del remitente y cambiar a (UIButton *) remitente) ..

Cambio

[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside]; 

a

[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpOutside]; 

y el método "changeButtonBackGroundColor"

[sender setBackgroundColor:[UIColor cyanColor]]; 

Segundo

crear UIControlEvents universales

[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventAllEvents]; 

-(void)changeButtonBackGroundColor:(UIButton*) sender{ 
if ([sender.backgroundColor isEqual:[UIColor redColor]]){ 
    [sender setBackgroundColor:[UIColor cyanColor]]; 
}else{ 
    [sender setBackgroundColor:[UIColor redColor]];   
}} 

no probé este código

Cuestiones relacionadas