iOS8 中的UIAlertController

iOS 8.3 之后UIAlertView,跟UIActionSheet都不被推薦使用了,雖然一直知道如此,但是因?yàn)槭掷锏睦洗a都是用的UIAlertView,所以從來沒真正使用過這個(gè)UIAlertController。今天寫一個(gè)Demo用到提示框,所以試一下。

UIAlertController

跟UIAlertView相比,UIAlertController不再需要實(shí)現(xiàn)代理方法,也無需指定按鈕;創(chuàng)建方法:

//創(chuàng)建控制器
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示:" message:@"我是一條信息" preferredStyle:UIAlertControllerStyleActionSheet];

特別注意第三個(gè)參數(shù),它決定了樣式是對(duì)話框(alert)還是上拉菜單(actionSheet)。

創(chuàng)建好控制器之后,通過UIAlertAction來給控制器添加動(dòng)作按鈕。

//創(chuàng)建動(dòng)作按鈕:
UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"OK  selected");
    }];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Cancel selected");
    }];
//把按鈕添加到控制器
[alertVC addAction:action0];

動(dòng)作按鈕樣式共三張:default,destructive,cancel;值得注意的是如果控制器的樣式選擇了actionSheet,那么它的destructive按鈕永遠(yuǎn)在最前面,無論添加順序是怎樣的,cancel則永遠(yuǎn)都在最后,而且只能有一個(gè)cancel類型的按鈕。

如果你的程序是寫在iPhone上的,那么很不幸你基本跟下面的內(nèi)容失之交臂了,而下面的內(nèi)容才是真正有趣的部分

由于我寫demo的時(shí)候用的設(shè)備是iPad,所以當(dāng)我通過上面的代碼嘗試actionSheet樣式的時(shí)候,我遇到了這個(gè)錯(cuò)誤:


屏幕快照 2016-01-20 23.11.51.png

那么這是怎么一回事呢?原來,在常規(guī)寬度的設(shè)備上,上拉菜單是以彈出視圖的形式展現(xiàn)。彈出視圖必須要有一個(gè)能夠作為源視圖或者欄按鈕項(xiàng)目的描點(diǎn)(anchor point)。而iOS8 之后,新出了一個(gè)UIPopoverPresentationController類來替代之前的UIPopoverController。

UIPopoverPresentationController

給UIAlertController配置popoverPresentationController:

    UIPopoverPresentationController *popover = alertVC.popoverPresentationController;
    if (popover) {
        popover.sourceView = sender;
        popover.sourceRect = sender.bounds;
        popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
    }
    
    [self presentViewController:alertVC animated:YES completion:nil];

iOS 8之后,我們不再需要給出彈出框的大小,UIAlertController將會(huì)根據(jù)設(shè)備大小自適應(yīng)彈出框的大小。并且在iPhone或者緊縮寬度的設(shè)備中它將會(huì)返回nil值。

配置好了popoverPresentationController,無論是在iPhone還iPad上,都沒問題咯。

最后,UIAlertController的popoverPresentationController取消了Canle樣式的action,因?yàn)橛脩敉ㄟ^點(diǎn)擊彈出視圖外的區(qū)域也可以取消彈出視圖,因此不需要了。

參考文章

我是源碼

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

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

  • iOS 8的新特性之一就是讓接口更有適應(yīng)性、更靈活,因此許多視圖控制器的實(shí)現(xiàn)方式發(fā)生了巨大的變化。全新的UIPre...
    烏拉拉zzZ閱讀 993評(píng)論 0 2
  • iOS 8的新特性之一就是讓接口更有適應(yīng)性、更靈活,因此許多視圖控制器的實(shí)現(xiàn)方式發(fā)生了巨大的變化。全新的UIPre...
    Tank丶Farmer閱讀 2,162評(píng)論 2 4
  • 轉(zhuǎn)載 http://www.cocoachina.com/ios/20141126/10320.html如果侵權(quán),...
    小劉_假裝是個(gè)程序員閱讀 703評(píng)論 0 0
  • 蘋果在iOS8.0后推出了UIAlertController以代替UIAlertView,導(dǎo)致的后果就是UIAle...
    LZM輪回閱讀 2,399評(píng)論 6 0
  • (一) “sir,叫我艾樂?!?“哦?!?那天我買了一個(gè)腎,總算擁有了她,iphone 6s,我不再會(huì)為同學(xué)聚會(huì)之...
    大喵w閱讀 541評(píng)論 0 1

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