- 在iOS8之后UIAlertView、UIActionSheet (以及它們各自的 delegate protocols)已經被棄用,在你的代碼中按住QQ圖片20141219102558.png點擊 UIAlertView 或者 UIActionSheet,你就會看到最上面的注釋:
NS_CLASS_DEPRECATED_IOS(2_0, 9_0, "UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead") __TVOS_PROHIBITED
NS_CLASS_DEPRECATED_IOS(2_0, 8_3, "UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead") __TVOS_PROHIBITED
本文章的主題就是 UIAlertController,向大家展示如何替換舊的 Alert,以及這些操作方法的高級擴展。
更詳細的內容參考:http://www.cocoachina.com/ios/20141219/10701.html
// 創建
UIAlertController *alertview=[UIAlertController alertControllerWithTitle:@"提示" message:@"Successful" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertControllerStyleActionSheet 是顯示在屏幕底部
// UIAlertControllerStyleAlert 是顯示在中間
// 設置按鈕
UIAlertAction *cancel=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *defult = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];
//UIAlertAction *destructive = [UIAlertAction actionWithTitle:@"destructive" style:UIAlertActionStyleDestructive handler:nil];
[alertview addAction:cancel];
[alertview addAction:defult];
//[alertview addAction:destructive];
//顯示(AppDelegate.h里使用self.window.rootViewController代替self)
[self presentViewController:alertview animated:YES completion:nil];
//[self.window.rootViewController presentViewController:alertview animated:YES completion:nil];