2012-08-17 7 views
10

Necesito poder quitar un botón de una vista y agregar uno diferente. Mi código se ve así:removeFromSuperview no funciona

-(void)UpdatePromoBanner:(NSString*)value{ 
    [button setTitle:@"newer text" forState:UIControlStateNormal]; 
    for (UIView *subView in emptyViewController.view.subviews) 
    { 
     if(subView.tag == 99) { 
      //--remove button and add an updated one 
      NSLog(@"Remove button?"); 
      [subView removeFromSuperview]; 
      //[subView.superview addSubview:button]; 
     } 
    } 
    NSLog(@"event called"); 

} 

-(void)AddPromoBannerToBottom:(UIView*)view { 

    button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button addTarget:self 
       action:@selector(aMethod:) 
    forControlEvents:UIControlEventTouchDown]; 
    [button setTitle:lblForBannerButton forState:UIControlStateNormal]; 
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); 
    button.tag = 99; 

    [view addSubview:button]; 
} 

emptyViewController es simplemente un controlador de vista simple vacía. Estoy agregando un botón en el medio. Pulso el NSLog ok que verifica la etiqueta, pero la vista no se elimina. Debo mencionar que estoy usando un hilo que está disparando el updatepromobanner cada 5 segundos.

+29

tiene que iniciar sus nombres de método con letras minúsculas. –

Respuesta

42

Oscar tiene razón. Tienes que actualizar la interfaz en el hilo principal. Supuse que agregaría algún código para ayudar.

Reemplazar:

[subView removeFromSuperview]; 

Con:

[subView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO]; 

y creo que debe ser bueno para ir sin cambiar nada más.

+0

estado volviendo loco por horas. ¡Gracias! – user987723

+0

Me alegra ayudar. Happy Coding :) –

+0

Hola @RyanPoolos, me preguntaba si qué pasa si en lugar de un botón, es un controlador de vistas separado que quisiera sacar? Cada vez que lo elimine, arrojaría un error. Si tiene tiempo, aquí hay una pregunta que publiqué hace varias horas. http://stackoverflow.com/questions/15490656/ipad-objective-c-using-removefromsuperview-to-remove-uicollectionviewcontroller ¡Gracias! – gdubs

8

No puede actualizar la IU usando una secuencia secundaria, cada vez que su hilo esté realizando actualizaciones de IU debe llamar al hilo principal.

5
dispatch_async(dispatch_get_main_queue(), ^{ 
     [subView removeFromSuperview]; 
}); 

Recuerde actualización de la interfaz de usuario en el hilo principal :)