UIAlertView for iOS 9的替代品?

use*_*007 16 objective-c uialertview ios uialertcontroller ios9

UAlertView在iOS 9及更高版本中已弃用.什么是另类?

UIAlertView *new = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[new show];
Run Code Online (Sandbox Code Playgroud)

Nit*_*itk 49

您可以使用此代码替换警报视图:

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];
Run Code Online (Sandbox Code Playgroud)

如果您需要多个操作,可以使用:

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];
Run Code Online (Sandbox Code Playgroud)

  • 这需要很多代码才能实现......他们在想什么?没有委托处理响应的能力是一个很好的补充. (2认同)

vad*_*ian 9

您可以获得详细信息,包括替换建议,方法是单击显示类/方法声明的符号.

如果UIAlertView你会看到

"不推荐使用UIAlertView.使用UIAlertController和UIAlertControllerStyleAlert的preferredStyle代替"