IOS 模态弹窗与操作版使用 UIAlertController
生活随笔
收集整理的這篇文章主要介紹了
IOS 模态弹窗与操作版使用 UIAlertController
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS8 以后UIAlertView 改用 UIAlertController 實現模態窗和操作板。UIAlertController 的使用與UIAlerView 非常不同,它實際上是把彈窗內容與顯示方式、按鈕列表、分離。實現起來非常簡單。如下
1.調用靜態方法創建彈窗控制器 alertControllerWithTitle
聲明彈窗控制器,title 表示彈窗的標題,message表示彈窗文字內容,重點是preferredStyle 表示彈窗的顯示方式,UIAlertControllerStyleActionSheet操作版方式顯示,UIAlertControllerStyleAlert 模態窗方式
// 創建控制器UIAlertController* alertConrtoll = [UIAlertController alertControllerWithTitle:@"錯誤" message:@"網絡錯誤,獲取失敗" preferredStyle:UIAlertControllerStyleActionSheet];2.為彈窗控制器增加按鈕 UIAlertAction
UIAlertActions 是彈窗按鈕類,通過靜態方法actionWithTitle 創建,style表示按鈕風格,handler是按鈕被點擊的回調函數。我們創建完按鈕組件通過 addAction加入彈窗控制器
// 創建彈窗按鈕組件UIAlertAction* okBtn = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler: nil];UIAlertAction* cancelBtn = [UIAlertAction actionWithTitle:@"重新獲取" style:UIAlertActionStyleCancel handler: nil];// 添加按鈕[alertConrtoll addAction:okBtn];[alertConrtoll addAction:cancelBtn];顯示彈窗
顯示彈窗和插入視圖控制器方法一致。
[self presentViewController:alertConrtoll animated:YES completion:nil];UIAlertController 屬性
| title | NSString | 標題 | |
| preferredStyle | UIAlertControllerStyle | 彈窗顯示方式,只讀 | |
| actions | NSArray<UIAlertAction *> | 彈窗按鈕列表,只讀 |
UIAlertAction 屬性
| enabled | BOOL | 是否啟用 | |
| title | NSString | 標題 | |
| style | UIAlertActionStyle | 按鈕風格 | UIAlertActionStyleDefault |
UIAlertController API
- + (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle 創建彈窗控制器并且設置標題,內容,顯示風格
- - (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler 添加可輸入彈窗
UIAlertAction API
- + (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler 創建彈窗按鈕并且設置標題和風格、處理事件
總結
以上是生活随笔為你收集整理的IOS 模态弹窗与操作版使用 UIAlertController的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOS UILabel组件
- 下一篇: Maven projects找回