彈出框在移動端開發(fā)中使用是比較頻繁的控件之一
1、在iOS8.0之前使用最多的原生彈出框控件是:UIAlertView啸驯,但是這個控件在iOS8.0之后蘋果公司就廢棄了這個方法
正如圖中所說:用UIAlertConntroller并設(shè)置樣式為UIAlertcontrollerStyleAlert 就是原來的UIAlertView了涉波,同理UIAlertcontrollerStyleActionSheet就是UIActionSheet
2英上、如果你不想使用UIAlertController,覺得使用起來不舒服怠蹂,那么你可以繼續(xù)使用UIAlertView與UIActionSheet善延,但是蘋果并不會對這兩個控件進行更新和維護了。所以城侧,作為一個開發(fā)人員易遣,還是接受新事物比好好點,畢竟自己也要成長
3嫌佑、使用方法如下:
類方法快速創(chuàng)建一個提示控制器 值得注意的是這個控制器有個preferreStyle屬性你可以根據(jù)這個屬性來確定是使用UIAlertView 還是 UIActionSheet
UIAlertControllerStyleActionSheet
UIAlertControllerStyleAlert
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"顯示的標(biāo)題" message:@"標(biāo)題的提示信息" 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就會調(diào)用 這個block");
}];
// 由于它是一個控制器 直接modal出來就好了
[self presentViewController:alertController animated:YES completion:nil];
由上述可見,省區(qū)了麻煩的代理方法屋摇,使用block揩魂,我們可以自行再次對其封裝 使用會更加方便。
我個人還是比較推崇block的炮温,所以火脉,這種還是比較習(xí)慣的