【问题标题】:How do I disable the Alert Add button in Xcode if the textField is empty?如果 textField 为空,如何禁用 Xcode 中的 Alert Add 按钮?
【发布时间】:2018-05-10 20:53:49
【问题描述】:

Xcode 9、iOS 11

我正在使用以下代码将文本添加到 iOS 中 TableView 应用程序的数组中:

@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
    var textField = UITextField()

    let alert = UIAlertController(title: "Add New Task", message: "", preferredStyle: .alert)

    let action = UIAlertAction(title: "Add Item", style: .default) { (action) in

      let newItem = Item(context: self.context)
      newItem.title = textField.text!
      newItem.done = false
      newItem.parentCategory = self.selectedCategory
      self.itemArray.append(newItem)

      self.saveItems()
    }

    alert.addTextField { (alertTextField) in
      alertTextField.placeholder = "Create new item"
      textField = alertTextField
    }

    alert.addAction(action)
    present(alert, animated: true, completion: nil)
  } 

代码运行良好,但有一个例外:即使 textField 为空白,也可以单击 Add Item。

如何在文本添加到文本字段之前禁用“添加项目”按钮?

【问题讨论】:

  • 您找到解决方案了吗?

标签: ios xcode


【解决方案1】:

我认为您无法做到这一点,因为 UIAlertController 的文本字段的代表甚至没有调用 UITextFieldDelegate 方法,这意味着您不知道何时为 isEnabled 设置 isEnabled 属性@ ,你可以去定制

【讨论】:

  • 那我就干脆放弃说做不到吗?或者当你说“你可以定制”时,你是说有解决方法吗?
猜你喜欢
  • 2019-07-10
  • 2018-11-28
  • 1970-01-01
  • 2018-11-06
  • 2016-10-28
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
相关资源
最近更新 更多