提示框是做项目时候经常遇到的一个功能,可以提高用户体验,除了一些著名的第三方库以外,我们更多会用到系统提供的提示框和上拉菜单。这里总结UIAlertView、UIActionSheet、UIAlertController的一些基础用法。
iOS8之前用的是UIAlertView、UIActionSheet,在iOS8之后UIAlertController就取代了前面两个。
UIAlertView
这个方法通过设置一个标题,内容和些按钮创建提示框,代码示例如下:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络请求失败" message:@"请检查网络设置" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
UIActionSheet
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"title" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"第一项",@"第二项", nil];
//设置样式
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.view];
UIAlertController
UIAlertController包含了UIActionSheet, UIAlertView的功能
提示框的功能,代码示例:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这个是UIAlertController的默认样式" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
上拉菜单的功能,代码示例:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"删除数据将不可恢复" preferredStyle: UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:nil];
UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:deleteAction];
[alertController addAction:archiveAction];
[self presentViewController:alertController animated:YES completion:nil];
如果对你有帮助,那就点个赞吧~