1.前段時間iOS升級後突然發現UIAlertView居然棄用啦,最後上網搜下才發現出現了衍生出新的控件UIAlertController,好吧!剛開始使用發現真的很不習慣,不過用一段時間也就習慣啦,這段時間使用發現UIAlertController相對於UIAlerView其實就是一下幾點不同:
1.棄用了代理開始使用代碼塊block,這裏一定要注意循環引用的問題
2.設置按鈕開始分開設置,不再像以前一樣一次設置完成啦!
其他的不同,我也沒怎麼發現!
言歸正傳,上代碼說說用法:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"我是標題" message:@"我是提示信息" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"取消執行");
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"確定1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"確定執行");
}];
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self presentViewController:alertController animated:YES completion:nil]
;
其運行效果是:
修改otherAction的樣式:UIAlertActionStyleDestructive 其運行效果爲:
修改UIAlertController的樣式:UIAlertControllerStyleActionSheet其運行效果:
如果有多個按鈕可通過增加:UIAlertAction來達到想要的效果