【问题标题】:How can I disable this done button until these two text fields have values?在这两个文本字段具有值之前,如何禁用此完成按钮?
【发布时间】:2017-07-22 23:39:01
【问题描述】:

我的目标是在这两个文本字段为空时禁用完成按钮。我有顶部按钮的逻辑,但只要顶部文本字段“taskNameTextFIeld”至少有一个字符,完成按钮就会启用。我不希望用户能够在“timeTextField”为空时按下完成。你可以在 textField() 中看到我目前拥有的内容。

import UIKit

protocol AddNewTaskViewControllerDelegate: class {
    func addNewTaskViewControllerDidCancel(_ controller: AddNewTaskViewController)
    func addNewTaskViewController(_ controller: AddNewTaskViewController, 
    didFinishAdding item: TaskData)
}

class AddNewTaskViewController: UITableViewController, UITextFieldDelegate {

@IBOutlet weak var taskNameTextFIeld: UITextField!
@IBOutlet weak var timeTextFIeld: UITextField!
@IBOutlet weak var doneBarButton: UIBarButtonItem!

weak var delegate: AddNewTaskViewControllerDelegate?

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let oldText = textField.text! as NSString
    let newText = oldText.replacingCharacters(in: range, with: string) as NSString
    doneBarButton.isEnabled = newText.length > 0
    return true
}

@IBAction func cancel(_ sender: Any) {
    delegate?.addNewTaskViewControllerDidCancel(self)
}

@IBAction func done(_ sender: Any) {
    let item = TaskData()
    item.task = taskNameTextFIeld.text!
    item.time = timeTextFIeld.text!

    delegate?.addNewTaskViewController(self, didFinishAdding: item)
}

func textFieldShouldReturn(_ TextField: UITextField) -> Bool {
    self.view.endEditing(true)
    return false
}

override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    return nil
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.taskNameTextFIeld.delegate = self;
    self.timeTextFIeld.delegate = self;
    // Do any additional setup after loading the view.
}
}

【问题讨论】:

  • 实际发生了什么?
  • 当我填写顶部文本字段时,doneBarButton 变为启用状态,我需要它保持禁用状态,直到两个字段都输入文本

标签: swift button tableview


【解决方案1】:

问题是您没有检查两个字段。变化:

doneBarButton.isEnabled = newText.length > 0

实际检查:

if taskNameTextFIeld.text.length > 0 && timeTextFIeld.text.length > 0 {
    doneBarButton.isEnabled = newText.length > 0
}

另外,我建议将两个文本字段的名称更改为 Field 而不是 Field

【讨论】:

  • 此代码在 shouldChangeCharactersIn 方法中无法正常工作。
  • 我这样做了,但我仍然遇到同样的问题。我还使用了(taskNameTextFIeld.text?.characters.count)! > 0,因为文本字段显然没有长度成员。也感谢您指出错字
  • 我自己打错了,因为我已经修复了,现在可以正常工作了,谢谢你的帮助!
【解决方案2】:

这里有一些关于我如何解决问题的细节。文本字段本身没有长度成员,所以我比较了字符数。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let oldText = textField.text! as NSString
    let newText = oldText.replacingCharacters(in: range, with: string) as NSString
    //doneBarButton.isEnabled = newText.length > 0
    if (taskNameTextFIeld.text?.characters.count)! > 0 && (timeTextFIeld.text?.characters.count)! > 0 {
        doneBarButton.isEnabled = newText.length > 0
    }
    return true
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 2022-01-22
    相关资源
    最近更新 更多