ios objective-c ios9 uialertview uialertcontroller

¿Alternativa a UIAlertView para iOS 9?



objective-c ios9 (7)

UAlertView está en desuso en iOS 9 y versiones posteriores. ¿Cuál sería una alternativa?

UIAlertView *new = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [new show];


A menudo se obtiene información detallada, incluida la sugerencia de reemplazo al hacer clic en el símbolo que muestra la declaración de clase / método.

En el caso de UIAlertView podrás ver

"UIAlertView está en desuso. Utilice UIAlertController con un styler preferido de UIAlertControllerStyleAlert"


He usado "UIAlertController" en iOS 8 y versiones posteriores. Vamos a ver:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" preferredStyle:UIAlertControllerStyleAlert];

Y añada botones:

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ //do something when click button }];

Recuerda:

[alertController addAction:okAction];

Entonces muéstralo:

[self presentViewController:alertController animated:YES completion:nill];

Si quieres mostrar un actionheep, cambias

"preferredStyle:UIAlertControllerStyleActionSheet"


Hice una categoría para eso:

+ (void)alertViewWithTitle:(NSString *)aTitle message:(NSString *)aMessage viewController:(UIViewController *) aVC { UIAlertController * alert = [UIAlertController alertControllerWithTitle:aTitle ? aTitle : @"" message:aMessage preferredStyle:UIAlertControllerStyleAlert]; UIViewController *topVC = aVC ? aVC : [UIApplication sharedApplication].keyWindow.rootViewController; [topVC presentViewController:alert animated:YES completion:nil]; }

Los parámetros aTitle y aVC son opcionales, pero se debe usar aVC si se conocen.

PD: evita el "nuevo" como nombre de variable esta es una palabra reservada, en realidad no sé si se compilará.


Puede usar este código para reemplazar una vista de alerta:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; [self presentViewController:alertController animated:YES completion:nil];

Si necesitas múltiples acciones puedes usar:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"Button 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // action 1 }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"Button 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // action 2 }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self dismissViewControllerAnimated:YES completion:nil]; }]]; [self presentViewController:alertController animated:YES completion:nil];



UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"Info" message:@"You are using UIAlertController" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [alert dismissViewControllerAnimated:YES completion:nil]; }]; UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [alert dismissViewControllerAnimated:YES completion:nil]; }]; [alert addAction:ok]; [alert addAction:cancel]; [self presentViewController:alert animated:YES completion:nil];


UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"My Title" message:@"Enter User Credentials" preferredStyle:UIAlertControllerStyleAlert]; [self presentViewController:alert animated:YES completion:nil];