背景:在UITableView的cell点击时弹出对话框并可以修改数据。由于UIAlertView在iOS9之后被过期,苹果建议使用UIAlertController来代替,接下来就尝试简单使用UIAlertController,目的效果:

1.创建UIAlertC的ontroller对象
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"修改" message:nil preferredStyle:UIAlertControllerStyleAlert];
2.添加文本输入框
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.text = model.name; // 这里的model是从plist加载来的模型数据
}];
这里要提到UIAlertController的一个属性:

这表示,可以添加多个UITextFiled,在界面设计上更加灵活。
3.添加UIAlertAction
//点击OK的action
UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//点击之后的动作,修改模型数据
UITextField * textFiled = alert.textFields[0];
model.name = textFiled.text;
// 修改模型中的数据后刷新cell
[_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}];
// 点击cancel按钮的action
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil];
// 把创建的anction添加到alert上
[alert addAction:cancelAction];
[alert addAction:okAction];
// 弹出对话框
[self presentViewController:alert animated:YES completion:nil];
网友评论