iOS中UIAlertController控件的使用

在開(kāi)發(fā)過(guò)程中我們平時(shí)使用的UIAlertView 提示的控件在ios9中已經(jīng)被廢棄使用了,而一直開(kāi)發(fā)以來(lái)用慣了這個(gè)控件的我盡管有些不舍,但是看著出現(xiàn)的警告,有強(qiáng)迫癥的我是不太能接受的,轉(zhuǎn)用了最新的控件UIAlertController。

?UIAlertController首先他是繼承于UIViewController,而UIAlertView是繼承于UIView的,從這點(diǎn)可以看出他們兩個(gè)的區(qū)別所在,而且很強(qiáng)大的是它將UIAlertControllerStyleActionSheet和UIAlertControllerStyleAlert收于它之下,也沒(méi)有什么代理啊等方法,廢話(huà)不說(shuō),直接上代碼演示。

```

UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"私信TA" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"私信TA");

}];

```

UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"不再看該內(nèi)容" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"不再看該內(nèi)容");

}];

UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"舉報(bào)該內(nèi)容" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"舉報(bào)該內(nèi)容");

}];

UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

}];

UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];//這里是警告類(lèi)型

[alertView addAction:action1];

[alertView addAction:action2];

[alertView addAction:action3];

[alertView addAction:action4];

[self presentViewController:alertView animated:YES completion:nil];

效果圖1:


圖1

如果將上面改成表單的話(huà)UIAlertControllerStyleActionSheet

效果圖2:


圖2

你要做一些操作的話(huà)可以在action的block里面做,再也不用寫(xiě)代理方法了。

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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