彈出框在移動端開發中使用是比較頻繁的控件之一
1、在iOS8.0之前使用最多的原生彈出框控件是:UIAlertView,但是這個控件在iOS8.0之后蘋果公司就廢棄了這個方法
正如圖中所說:用UIAlertConntroller并設置樣式為UIAlertcontrollerStyleAlert 就是原來的UIAlertView了,同理UIAlertcontrollerStyleActionSheet就是UIActionSheet
2、如果你不想使用UIAlertController,覺得使用起來不舒服,那么你可以繼續使用UIAlertView與UIActionSheet,但是蘋果并不會對這兩個控件進行更新和維護了。所以,作為一個開發人員,還是接受新事物比好好點,畢竟自己也要成長
3、使用方法如下:
類方法快速創建一個提示控制器 值得注意的是這個控制器有個preferreStyle屬性你可以根據這個屬性來確定是使用UIAlertView 還是 UIActionSheet
UIAlertControllerStyleActionSheet
UIAlertControllerStyleAlert
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"顯示的標題" message:@"標題的提示信息" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//所有的點擊按鈕的操作都在此處處理,
NSLog(@"點擊取消");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點擊確認");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"警告" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點擊警告");
}]];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
NSLog(@"添加一個textField就會調用 這個block");
}];
// 由于它是一個控制器 直接modal出來就好了
[self presentViewController:alertController animated:YES completion:nil];
由上述可見,省區了麻煩的代理方法,使用block,我們可以自行再次對其封裝 使用會更加方便。
我個人還是比較推崇block的,所以,這種還是比較習慣的