UIAlertViewDelegate 的使用简介


---------------"编辑"模式: 更改名字 

// 弹出alerView
   
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"编辑"
                                                      
message:@""
                                                     
delegate:self
                                            
cancelButtonTitle:@"取消"
                                            
otherButtonTitles:@"确定", nil];
    // 设置alertView style
   
/**
     UIAlertViewStyleDefault = 0,
     UIAlertViewStyleSecureTextInput,
安全输入
     UIAlertViewStylePlainTextInput, 
文本输入
     UIAlertViewStyleLoginAndPasswordInput 
登录
     */
    alerView.alertViewStyle = UIAlertViewStylePlainTextInput;// 文本输入
  
   
// 1. 获取当前被点击cell 所对应的 模型
   
HeroModel *model = self.dataArray[indexPath.row];
   
   
// 2. 把英雄的名字, 显示到 textField
   
   
// 获取 alertView 指定位置   textFiled
   
UITextField *textFiled = [alerView textFieldAtIndex:0];
   
    textFiled.
text = model.name;
   
   
// 设置当前aletView tag   indexPath.row
    alerView.
tag = indexPath.row;
   
    [alerView
show];
    
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
   
   
if (buttonIndex == 1) { // 点击了确定按钮
       
       
// 1. 获取alertView 输入框中的内容
       
UITextField *textField = [alertView textFieldAtIndex:0];
       
       
NSString *newName = textField.text;
       
       
// 2. 刷新数据
       
// 2.1 修改数据源中 被点击cell 对应的 模型对象中 name 修改为  newName
       
// 先取出 alertView tag
       
NSInteger index  = alertView.tag;
       
       
// 取出index 对应到数组中的元素
       
HeroModel *model = self.dataArray[index];
       
        model.
name = newName;
       
       
// 2.2 刷新数据
       
// 这个刷新所有数据
//        [_tableView reloadData];
       
// 刷新指定cell 数据
       
// 创建NSIndexPath
       
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
       
       
/**
         AtIndexPaths:
传递 一个数组 数组中包含  NSIndexPath
         withRowAnimation :
动画效果
         */

        [
_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
       
    }
}



---------------"编辑"模式: 更改名字 

pragma mark -  点击cell的时候调用的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
   
// 0. 取出当前cell对应的model
   
HeroModel *model = self.dataArray[indexPath.row];
   
   
// 使用alertController
   
// 1. 实例化一个alertController
   
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Edit" message:nil preferredStyle:UIAlertControllerStyleAlert];
   
   
// 2. 添加文本输入框
    [alertController
addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
       
        textField.
text = model.name;
       
    }];
   
   
   
// 3. 添加action
   
// 3. 添加 cacel action
   
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
   
   
// 添加 cancel action alertControler
    [alertController
addAction:cancelAction];
   
   
// 4. 添加sureAction
   
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      
       
// 获取到修改后的文本
       
UITextField *field = [alertController textFields].lastObject;
       
       
NSString *newName = field.text;
       
       
// 修改数据源中的 模型 对象中的 name属性
        model.
name = newName;
       
       
// 刷新数据
        [
_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
       
    }];
   
   
// sureAction 添加到控制器
    [alertController
addAction:sureAction];
   
   
   
   
// 展示alertCOntroller
    [
self presentViewController:alertController animated:YES completion:nil];
 
   
}


// 隐藏状态栏
- (
BOOL)prefersStatusBarHidden {
   
return YES;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值