Альтернатива UIAlertView для iOS 9?
UAlertView
устарел в iOS 9 и более поздних версиях. Что было бы альтернативой?
UIAlertView *new = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[new show];
Ответы
Ответ 1
Вы можете использовать этот код, чтобы заменить вид предупреждения:
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];
Если вам нужно несколько действий, вы можете использовать:
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];
Ответ 2
Вы часто получаете подробную информацию, в том числе предложение о замене на ⌘ -clicking на символе, который отображает объявление класса/метода.
В случае UIAlertView
вы увидите
"UIAlertView устарел. Используйте UIAlertController с предпочтительным типом UIAlertControllerStyleAlert вместо"
Ответ 3
UIAlertController существует с iOS 8.
Ответ 4
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"My Title"
message:@"Enter User Credentials"
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
Ответ 5
У меня есть "UIAlertController" на iOS 8 и более поздних версиях. Давайте посмотрим:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" preferredStyle:UIAlertControllerStyleAlert];
И добавьте кнопки:
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//do something when click button
}];
Помните:
[alertController addAction:okAction];
Затем покажите это:
[self presentViewController:alertController animated:YES completion:nill];
Если вы хотите показать actionheep, вы меняете
"preferredStyle:UIAlertControllerStyleActionSheet"
Ответ 6
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];