Centauro12
Centauro12

Reputation: 41

How do I display a button on a UIAlertView after displaying it

I am newbie developing with objective C.

I am working on a piece of code that installs a plugin to my application. It downloads a .zip package, uncompress it and copy some data to my sqlite database.

I have a UIAlertView that shows a UIProgressView while the app is downloading and uncompressing, when it finish I add to the UIAlertView a button with the addButtonWithTitle method.

I dont know why my button appears on the top-left corner of my UIAlertView.

This is a piece of my code:

ventana = [[UIAlertView alloc] initWithTitle:[[NSString alloc] initWithFormat: @"Instalando %@", codigo] 
                                             message:@"Por favor, no apague el dispositivo ni cierre la aplicación." delegate:nil cancelButtonTitle:nil  otherButtonTitles: nil];

actividad = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
actividad.frame = CGRectMake(20, 110, 20, 20);

progreso = [[UIProgressView alloc] initWithFrame:CGRectMake(50, 115, 215, 9)];
[ventana addSubview:actividad];
[actividad startAnimating];
[ventana addSubview:progreso];
[ventana show];

-- some stuff (downloading, uncompressing, updating my UIProgressView...) --
[progreso removeFromSuperview];
[actividad removeFromSuperview];
[ventana addButtonWithTitle:@"Aceptar"];
ventana.message = @"Instalación finalizada";

I have a image but I can't post it here because I am a new user.

Anyone knows why my button appears at the top-left corner of my UIAlertView (ventana) Thanks!

Upvotes: 0

Views: 1714

Answers (1)

Ed Marty
Ed Marty

Reputation: 39690

The way you are using the UIAlertView is sort of... bad. You should nevershow a UIAlertView with 0 buttons, and using it as a 'don't turn off the device' message is a bad idea. The "accepted" use for the alert view is to tell the user something important has just happened. If you insist, however, you should have a cancel button in there by default so they can stop the operation if they want to, then when it's finished, add the button. The UIAlertView may be confused when trying to add a button to a list of buttons that doesn't exist (because you initialized it with 0 buttons).

However, the better way to go about it would be to show a progress indicator while downloading (which you have) on a general UIView with a UILabel containing the message. Then, when it's completed, change the label to show your "Instalación finalizada" message, and display a button below it. I know, it seems like it's just replicating what you already have, but there's nowhere in your description that I see that calls for the use of a UIAlertView.

Upvotes: 3

Related Questions