iOS UIAlertController學(xué)習(xí)筆記

在最近學(xué)習(xí)的iOS開發(fā)中用到了一些警告提示框,看到的很多教程里用的還是UIAlertview,但是iOS9.0開始,xcode里已經(jīng)明確提示了不推薦使用,而是用UIAlertController來代替。
記錄一下我用到的幾種情況:

1.普通的警告提示框,顯示在屏幕正中心,有一個標(biāo)題,一段內(nèi)容,以及一個確定按鈕和一個取消按鈕:

NSString *title = @"這是標(biāo)題";  
NSString *message = @"這是提示內(nèi)容";  
NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil);  
NSString *otherButtonTitle = NSLocalizedString(@"確定", nil);  
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title  
                                                                         message:message  
                                                                  preferredStyle:UIAlertControllerStyleAlert];  
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle  
                                                       style:UIAlertActionStyleCancel  
                                                     handler:^(UIAlertAction *action) {}];  
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle  
                                                      style:UIAlertActionStyleDefault  
                                                    handler:^(UIAlertAction *action) {  
                                                        [self startAgain];  
                                                    }];  
[alertController addAction:cancelAction];  
[alertController addAction:otherAction];  
[self presentViewController:alertController animated:YES completion:nil]; 

當(dāng)然也可以根據(jù)實際情況去掉一個按鈕。

2.從下方向上顯示的,多用于選擇相機(jī)還是相冊這類情況:

UIAlertController *_alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[_alertController addAction:[UIAlertAction actionWithTitle:@"拍攝" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    [self takePhoto];
}]];
[_alertController addAction:[UIAlertAction actionWithTitle:@"從相冊選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    [self choosePhotoFromLibrary];
}]];
[_alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    _alertController = nil;
}]];
[self presentViewController:_alertController animated:YES completion:nil];

暫時我就用到了這兩種情況,當(dāng)然還有帶一個輸入框的,以及多個輸入框的,帶有警示按鈕的等等,這些都還沒用到,等用到了再記錄下來。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,161評論 4 61
  • iOS 8的新特性之一就是讓接口更有適應(yīng)性、更靈活,因此許多視圖控制器的實現(xiàn)方式發(fā)生了巨大的變化。全新的UIPre...
    烏拉拉zzZ閱讀 993評論 0 2
  • 今天聽了好幾遍成甲講的《微習(xí)慣》這本書,發(fā)現(xiàn)微習(xí)慣跟自控力關(guān)系匪淺,立即跟大家分享之。 這本書的作者斯蒂芬蓋斯不是...
    肖爺_族長閱讀 628評論 6 11
  • 2017.06.12 天氣陰晴不定 最近很焦躁,心里總是隱隱藏著一座活火山,一直叫囂著要爆發(fā)。手上的佛珠捏...
    violefay閱讀 587評論 0 1
  • 烏金: 你好。一直以來,我覺得說話是要表達(dá)心聲的,尤其是在自己的地盤。微信朋友圈算是自個兒的地盤吧,在自己的地盤聊...
    彼得貓烏金閱讀 372評論 2 48

友情鏈接更多精彩內(nèi)容