如何在声明闭包外修改UITextField的placeholder,实现空输入提示?
实现UITextField为空时修改UIAlertController的placeholder
要实现点击"Add Item"按钮时,若输入为空就修改文本框的placeholder,你只需要在代码的else分支中直接修改已捕获的UITextField的placeholder属性即可,还可以添加震动动画增强用户提示效果。
修改后的核心代码
在addButtonPressed方法的UIAlertAction闭包的else分支中添加以下代码:
else { // 修改placeholder文本 textField.placeholder = "Enter something here" // 可选:添加文本框震动动画,提示用户输入 let shakeAnimation = CABasicAnimation(keyPath: "position") shakeAnimation.duration = 0.05 shakeAnimation.repeatCount = 5 shakeAnimation.autoreverses = true shakeAnimation.fromValue = NSValue(cgPoint: CGPoint(x: textField.center.x - 5, y: textField.center.y)) shakeAnimation.toValue = NSValue(cgPoint: CGPoint(x: textField.center.x + 5, y: textField.center.y)) textField.layer.add(shakeAnimation, forKey: "shake") }
完整的修改后代码
import UIKit class TodoListViewController: UITableViewController { var itemArray = ["A", "B", "C"] override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white, .font: UIFont.systemFont(ofSize: 25, weight: .bold)] self.navigationController?.view.backgroundColor = UIColor(red: 0.29, green: 0.63, blue: 0.82, alpha: 1) } //MARK: - TableView DataSource Methods override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return itemArray.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoItemCell", for: indexPath) cell.textLabel?.text = itemArray[indexPath.row] return cell } //MARK: - TableView Delegate Methods override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if tableView.cellForRow(at: indexPath)?.accessoryType == .checkmark{ tableView.cellForRow(at: indexPath)?.accessoryType = .none } else { tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark } tableView.deselectRow(at: indexPath, animated: true) } //MARK: - Add New Items @IBAction func addButtonPressed(_ sender: UIBarButtonItem) { var textField = UITextField() let alert = UIAlertController(title: "Add New Item:", message: "", preferredStyle: .alert) let action = UIAlertAction(title: "Add Item", style: .default) { action in if let inputText = textField.text, !inputText.isEmpty { self.itemArray.append(inputText) self.tableView.reloadData() } else { // 修改placeholder并添加震动提示 textField.placeholder = "Enter something here" let shakeAnimation = CABasicAnimation(keyPath: "position") shakeAnimation.duration = 0.05 shakeAnimation.repeatCount = 5 shakeAnimation.autoreverses = true shakeAnimation.fromValue = NSValue(cgPoint: CGPoint(x: textField.center.x - 5, y: textField.center.y)) shakeAnimation.toValue = NSValue(cgPoint: CGPoint(x: textField.center.x + 5, y: textField.center.y)) textField.layer.add(shakeAnimation, forKey: "shake") } } alert.addTextField { alertTextField in alertTextField.placeholder = "Create new item" textField = alertTextField } alert.addAction(action) present(alert, animated: true, completion: nil) } }
关键说明
- 我们通过提前捕获的
textField引用,直接修改其placeholder属性,由于alert已经处于显示状态,修改后会立即更新界面 - 新增的震动动画是可选增强效果,能更直观地提醒用户需要输入内容
- 把原代码中的
textField.text != ""改成了更安全的if let inputText = textField.text, !inputText.isEmpty,避免强制解包的潜在问题
内容的提问来源于stack exchange,提问作者Twisha Solgama
相关产品推荐
相关产品推荐

