alertas alerta ios iphone uialertview uiactivityindicatorview

ios - alerta - uialertcontroller swift 4



¿Cómo puedo mostrar alerta de alerta con indicador de actividad? (7)

Quiero mostrar alerta con mensaje: "Cargando datos" e indicador de actividad de giro. ¿Cómo puedo hacer esto?


Agregue esto en su archivo .h UIAlertView *connectingAlert;

Y agrega estas dos funciones en tus archivos .m

//show loading activity. - (void)startSpinner:(NSString *)message { // Purchasing Spinner. if (!connectingAlert) { connectingAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(message,@"") message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; connectingAlert.tag = (NSUInteger)300; [connectingAlert show]; UIActivityIndicatorView *connectingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; connectingIndicator.frame = CGRectMake(139.0f-18.0f,50.0f,37.0f,37.0f); [connectingAlert addSubview:connectingIndicator]; [connectingIndicator startAnimating]; } } //hide loading activity. - (void)stopSpinner { if (connectingAlert) { [connectingAlert dismissWithClickedButtonIndex:0 animated:YES]; connectingAlert = nil; } // [self performSelector:@selector(showBadNews:) withObject:error afterDelay:0.1]; }

luego llame

[self startSpinner:@"Your message........"]; [self stopSpinner];


En swift 3

let loadingAlertController = UIAlertController(title: "Loading", message: nil, preferredStyle: .alert) let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray) activityIndicator.translatesAutoresizingMaskIntoConstraints = false loadingAlertController.view.addSubview(activityIndicator) let xConstraint = NSLayoutConstraint(item: activityIndicator, attribute: .centerX, relatedBy: .equal, toItem: loadingAlertController.view, attribute: .centerX, multiplier: 1, constant: 0) let yConstraint = NSLayoutConstraint(item: activityIndicator, attribute: .centerY, relatedBy: .equal, toItem: loadingAlertController.view, attribute: .centerY, multiplier: 1.4, constant: 0) NSLayoutConstraint.activate([ xConstraint, yConstraint]) activityIndicator.isUserInteractionEnabled = false activityIndicator.startAnimating() let height: NSLayoutConstraint = NSLayoutConstraint(item: loadingAlertController.view, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 80) loadingAlertController.view.addConstraint(height); self.present(loadingAlertController, animated: true, completion: nil)


puede agregar una etiqueta y un indicador de actividad como subvistas de su vista de alerta. Tienes que hacer algo como esto ...

- (IBAction)showAlertWithActivity:(id)sender{ alerta = [[UIAlertView alloc] initWithTitle:@"Guardando datos..." message:@"/n" delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn''t blur [alerta addSubview:spinner]; [spinner startAnimating]; [alerta show]; [self performSelector:@selector(close) withObject:self afterDelay:1]; } -(void)close{ [alerta dismissWithClickedButtonIndex:0 animated:YES]; }


puede agregar una etiqueta y un indicador de actividad como subvistas de su vista de alerta. tienes que hacer algo como esto

myAlertView = [[UIAlertView alloc] initWithTitle:@"Loading" message:@"/n/n" delegate:self cancelButtonTitle:@"" otherButtonTitles:@"OK", nil]; UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; loading.frame=CGRectMake(150, 150, 16, 16); [myAlertView addSubview:loading]; [myAlertView show];

..mejor para usar un UIActionSheet en esta situación ...


Esto funciona en iOS 7

addSubView no funciona en UIAlertView en iOS 7 y superior. Prueba esto en su lugar

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Loading data" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil]; UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; [indicator startAnimating]; [alertView setValue:indicator forKey:@"accessoryView"]; [alertView show];

y despedirlo

[alertView dismissWithClickedButtonIndex:0 animated:YES];


NOTA: Esta solución no funcionará en iOS 7 y superior.

Esta es mi opinión sobre ella:

alertView = [[UIAlertView alloc] initWithTitle:@"Submitting Entry" message:@"/n" delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn''t blur [alertView addSubview:spinner]; [spinner startAnimating]; [alertView show];

y descartar en código utilizando:

[alertView dismissWithClickedButtonIndex:0 animated:YES];


UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alarm" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil]; UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; loading.frame=CGRectMake(125, 50, 36, 36); [loading startAnimating]; [alert addSubview:loading]; [alert show];