Pregunta

Aquí está mi código ...

- (void) threadStartAnimating {
    [activityIndicator startAnimating];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    activityIndicator=[[UIActivityIndicatorView alloc] init];
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicator.center=[self tableView].center;
    [[self tableView] addSubview:activityIndicator];

    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
    EventController *eventController = [[EventController alloc] init];
    [eventController setEventID:[[lineItems objectAtIndex:[indexPath row]] objectForKey:@"view_event_button_param"]];
    [activityIndicator stopAnimating];
    [[self navigationController] pushViewController:eventController animated:YES];
}

El controlador de eventos contiene un servicio web que tarda unos segundos en cargarse. La única forma en que puedo obtener el indicador de actividad para mostrar es si lanza un bucle infinito allí ... Encontré un montón de ejemplos que mencionaron ponerlo en un hilo separado pero no parece funcionar.

¿Fue útil?

Solución

Su problema es que si está obteniendo los datos viewDidLoad Entonces, ese método no se llamará hasta que se cargue la vista, que generalmente es durante el proceso de mostrar primero ese controlador de vista. En su caso, esto es durante el pushViewController:animated: llamar.

Entonces, una forma de hacer lo que quiere podría ser cambiar alrededor de estas dos líneas:

[activityIndicator stopAnimating];
[[self navigationController] pushViewController:eventController animated:YES];

Pero, con toda honestidad, crearía un protocolo llamado EventControllerDelegate, tienen una propiedad de referencia débil a un id <EventControllerDelegate> en tus EventController y luego configure el controlador de vista que crea el EventController como el delegado antes de presionarlo. Luego, defina un método en el delegado e impleméntelo en su controlador de vista y en ese método detenga la ruleta. Luego en EventController, cuando haya terminado de cargar sus datos, llame al método delegado.

EDITAR: Si realmente desea hacerlo sin definir un delegado, podría intentar cambiar su código a algo como esto:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    activityIndicator=[[UIActivityIndicatorView alloc] init];
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicator.center=[self tableView].center;
    [[self tableView] addSubview:activityIndicator];

    EventController *eventController = [[EventController alloc] init];
    [eventController setEventID:[[lineItems objectAtIndex:[indexPath row]] objectForKey:@"view_event_button_param"]];

    [activityIndicator startAnimating];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIView *v = eventController.view;

        dispatch_async(dispatch_get_main_queue(), ^{
            [activityIndicator stopAnimating];
            [[self navigationController] pushViewController:eventController animated:YES];
        });
    });
}

Está utilizando GCD para mayor claridad y el habitual "acceda a la propiedad de la vista para forzar un loadView / viewDidLoad" cortar a tajos.

Otros consejos

Necesita establecer un tamaño de marco para su activityIndicator.

activityIndicator.frame = CGRectMake(x, y, 40, 40);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top