IOS開發(fā) UIAlertController詳解及實例代碼
IOS開發(fā) UIAlertController詳解
在iOS 8.0后,蘋果棄用了UIAlertView和UIActionSheet,轉(zhuǎn)而使用UIAlertController把之前的UIAlertView和UIActionSheet整合在一起。新版的API變得簡潔了不少幾行代碼就可實現(xiàn)之前一大片代碼的功能
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert" message:@"This is an alert." preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { NSLog(@"你好你好"); }]; UIAlertAction* defaultAction2 = [UIAlertAction actionWithTitle:@"OK2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { NSLog(@"你好你好"); }]; [alert addAction:defaultAction]; [alert addAction:defaultAction2]; [self presentViewController:alert animated:YES completion:nil];
初始化AlertView沒有太大區(qū)別,主要區(qū)別就是添加事件。蘋果公司新添加了UIAlertAction專門用來添加事件。一個Action對應(yīng)一個事件,添加到alert上就可以使用。
切換為ActionSheet只需要修改preferredStyle為UIAlertControllerStyleActionSheet
也可以添加輸入框代碼如下
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"輸入用戶名"; }];
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
詳解iOS應(yīng)用UI開發(fā)中的九宮格坐標計算與字典轉(zhuǎn)換模型
這篇文章主要介紹了iOS應(yīng)用UI開發(fā)中的九宮格坐標計算與字典轉(zhuǎn)換模型,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01詳解iOS App中UISwitch開關(guān)組件的基本創(chuàng)建及使用方法
UISwitch組件就是我們平時在iOS設(shè)置菜單中開到的那種左右滑動的開關(guān)按鈕,當(dāng)然我們在開發(fā)時可以進行更多的自定義,這里我們就來詳解iOS App中UISwitch開關(guān)組件的基本創(chuàng)建及使用方法2016-05-05iOS11 SectionHeader 胡亂移動且滑動時出現(xiàn)重復(fù)內(nèi)容的解決方法
這篇文章主要介紹了iOS11 SectionHeader 胡亂移動且滑動時出現(xiàn)重復(fù)內(nèi)容的解決方法,需要的朋友可以參考下2017-11-11