UIAlertController 和 UIAlertView的使用
来源:互联网 发布:关羽 知乎 编辑:程序博客网 时间:2024/05/29 07:47
UIAlertView 和 UIAlertController 都可以实现弹框的效果,但是在iOS 8之后,推荐使用UIAlertController,接下来我们就探讨一下,它们之间使用的异同点。
需求:在工作中经常会遇到点击UITableViewCell的某一行,弹出一个框,然后修改里面的内容
实现:
1>使用UIAlertController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{<pre name="code" class="objc"> //1.取出这一行的模型数据 WTHeroModel * model = self.heroArray[indexPath.row]; //2.取得模型数据的名称 NSString * heroName = model.name; //3.实例化UIAlertController UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"编辑" message:@"请输入要修改的内容" preferredStyle:UIAlertControllerStyleAlert]; //4.给UIAlertController添加一个textField [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//给textField赋值
textField.text = heroName; }];
//5.显示UIAlertController [self presentViewController:alertController animated:YES completion:nil]; //6.添加确定,取消按钮 UIAlertAction * CancelAction = [UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleCancel handler:nil];
[alertController addAction:CancelAction];
UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField * txtFields = [alertController.textFields lastObject]; //修改模型数据 model.name = txtFields.text; //7.刷新表格 [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];//单行刷新
<span style="white-space:pre"></span>//[self.tableView reloadData]; 全部刷新 }]; [alertController addAction:sureAction];
}
2>使用UIAlertView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //1.取出模型 WTHeroModel * model = self.heroArray[indexPath.row]; //2.取出英雄名称 NSString * name = model.name; //3.实例化弹框 UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@" 编辑" message:@"<span style="font-family: Arial, Helvetica, sans-serif;">请输入要修改的内容</span><span style="font-family: Arial, Helvetica, sans-serif;">" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];</span> /**
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="color:#ff0000;"> UIAlertViewStyleDefault = 0, 默认</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="color:#ff0000;"> UIAlertViewStyleSecureTextInput, 密码输入</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="color:#ff0000;"> UIAlertViewStylePlainTextInput, 文本框</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="color:#ff0000;"> UIAlertViewStyleLoginAndPasswordInput 账号,密码输入</span></p>
*/ alert.alertViewStyle = UIAlertViewStylePlainTextInput; //4.添加文本框 UITextField * field = [alert textFieldAtIndex:0]; alert.tag = indexPath.row; field.text = name; [alert show];}遵守UIAlertView协议,实现协议中的方法<UIAlertViewDelegate>
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == 1) { //取得修改后的值 UITextField * field = [alertView textFieldAtIndex:0]; NSString * name = <span style="font-family: Arial, Helvetica, sans-serif;">field</span>.text; WTHeroModel * model = self.array[alertView.tag]; model.name = name; //刷新数据 NSIndexPath * indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0]; [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];<span style="white-space:pre"></span>}}
两种方法都可以实现,第一种使用的是block,第二种是用的是代理,个人感觉还是block使用的比较顺手一些。
0 0
- UIAlertController 和 UIAlertView的使用
- UIAlertView和UIAlertController的使用
- UIAlertView和UIAlertController的封装
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet)
- UIAlertView 和UIAlertController二合一
- UIAlertView 和 UIAlertController
- UIAlertController和UIAlertView
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- 内存管理
- [BZOJ1009][HNOI2008]GT考试
- 读《疯狂Java讲义》笔记总结三
- C#精确判断一个人的年龄
- poj--3620--Avoid The Lakes(dfs)
- UIAlertController 和 UIAlertView的使用
- 5-5 UVA 10391 Compound Words复合词
- Windows下JDK安装环境变量配置
- 【J】Activity介绍
- Django中Request 对象介绍
- 去除透传,转义字符
- eval()
- Android AbsListView坐标体系解析
- ubuntu tftp