一個網(wǎng)管的iOS學(xué)習(xí)筆記,記錄下自己這條路上的點點滴滴。都是一些很簡單的筆記,不敢妄談教學(xué),純粹只是為了記錄自己在這條路上——前進著。
UIAlertController是iOS8推出的新概念,取代了之前的 UIAlertView和UIActionSheet(雖然現(xiàn)在仍可以使用,但是會有警告)。
UIAlertController 同時替代了 UIAlertView 和 UIActionSheet,從系統(tǒng)層級上統(tǒng)一了 alert 的概念 —— 即以 modal 方式或 popover 方式展示。
UIAlertController 是 UIViewController 的子類,而非其先前的方式。因此新的 alert 可以由 view controller 展示相關(guān)的配置中獲益很多。
UIAlertController 不管是要用 alert 還是 action sheet 方式展示,都要以 title 和 message 參數(shù)來初始化。Alert 會在當前顯示的 view controller 中心以模態(tài)形式出現(xiàn),action sheet 則會在底部滑出。Alert 可以同時有按鈕和輸入框,action sheet 僅支持按鈕。
新的方式并沒有把所有的 alert 按鈕配置都放在初始化函數(shù)中,而是引入了一個新類 UIAlertAction 的對象,在初始化之后可以進行配置。這種形式的 API 重構(gòu)讓對按鈕數(shù)量、類型、順序方便有了更大的控制。同時也棄用了 UIAlertView 和 UIActionSheet 使用的delegate 這種方式,而是采用更簡便的完成時回調(diào)。
正好上面這篇教程是swift的,所以我就寫一下Objective-C的吧。
一)新舊對比:
標準的Alert樣式:

- 舊方法:UIAlertView:
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"沒有標題的標題"
message:@"學(xué)無止境,漫漫長路"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"確定", nil];
//顯示alertView
[alertView show];
- 新方法:UIAlertController:
//UIAlertController風(fēng)格:UIAlertControllerStyleAlert
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"沒有標題的標題"
message:@"學(xué)無止境,漫漫長路"
preferredStyle:UIAlertControllerStyleAlert ];
//添加取消到UIAlertController中
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
//添加確定到UIAlertController中
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:OKAction];
[self presentViewController:alertController animated:YES completion:nil];
標準的Alert Sheet樣式:

- 舊方法:UIActionSheet
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"標準的Action Sheet樣式"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:@"了解更多"
otherButtonTitles:@"原來如此", nil];
[actionSheet showInView:self.view];
- 新方法:UIAlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標準的Action Sheet樣式"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
//取消:style:UIAlertActionStyleCancel
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelAction];
//了解更多:style:UIAlertActionStyleDestructive
UIAlertAction *moreAction = [UIAlertAction actionWithTitle:@"了解更多" style:UIAlertActionStyleDestructive handler:nil];
[alertController addAction:moreAction];
//原來如此:style:UIAlertActionStyleDefault
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"原來如此" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:OKAction];
[self presentViewController:alertController animated:YES completion:nil];
二)新功能:
UIAlertController 并不只是對已有的 API 做了清理,而是進行了標準化歸納。以前,預(yù)設(shè)的樣式閑置有很多(swizzling 雖然可以提供更多的功能但還是有很大風(fēng)險)。UIAlertController 讓以前看起來很神奇的事情變?yōu)榱丝赡堋?/code>
ps:摘自——UIAlertController - NSHipster
帶有警示按鈕的 Alert:

- 這種行為已經(jīng)被UIAlertActionStyle所覆蓋,共有三種類型:
style:UIAlertActionStyleDefault//對按鈕應(yīng)用標準樣式
style:UIAlertActionStyleCancel//對按鈕應(yīng)用取消樣式,即取消操作
style:UIAlertActionStyleDestructive//對按鈕應(yīng)用警示性樣式,提示用戶這樣做可能會刪除或者改變某些數(shù)據(jù)
- 所以想要對模態(tài)的 alert 加一個警示性的按鈕,只需要加上 .Destructive 風(fēng)格的 UIAlertAction 屬性:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"這是標題啊標題-標題"
message:@"這是消息啊消息-消息"
preferredStyle:UIAlertControllerStyleAlert ];
//取消style:UIAlertActionStyleDefault
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
//簡直廢話:style:UIAlertActionStyleDestructive
UIAlertAction *rubbishAction = [UIAlertAction actionWithTitle:@"簡直廢話" style:UIAlertActionStyleDestructive handler:nil];
[alertController addAction:rubbishAction];
[self presentViewController:alertController animated:YES completion:nil];
有很多個按鈕的Alert:

- 有 1 個或者 2 個操作的時候,按鈕會水平排布。更多按鈕的情況,就會像 action sheet 那樣展示:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"今天去誰家蹭飯呢"
message:@"??????\n話說可以去好多好多妹紙家里蹭飯"
preferredStyle:UIAlertControllerStyleAlert ];
UIAlertAction *home1Action = [UIAlertAction actionWithTitle:@"去小紅家" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:home1Action];
UIAlertAction *home2Action = [UIAlertAction actionWithTitle:@"去小蘭家" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:home2Action];
UIAlertAction *home3Action = [UIAlertAction actionWithTitle:@"去小花家" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:home3Action];
UIAlertAction *home4Action = [UIAlertAction actionWithTitle:@"去小嬌家" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:home4Action];
//取消style:UIAlertActionStyleDefault
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消(回家)" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
還有“創(chuàng)建登錄表單”和“創(chuàng)建注冊表單”兩塊內(nèi)容,基本上理解怎么實現(xiàn),就放明天繼續(xù)完善吧。睡覺!