用户从UITextfield清除文本时禁用按钮

时间:2018-10-31 10:54:20

标签: ios swift uitextfield uitextfielddelegate

当第一次加载视图时,我可以禁用按钮。即使textfield为空。但是,在发送消息后,当我键入文本并从textfield中删除文本时,它将无法正常工作。 button交互仍然有效,用户可以发送不需要的空消息。我仍然希望在用户键入和删除文本时仍然禁用该按钮,以防他们改变主意。这是我的代码。

@IBOutlet weak var textField: UITextField!
@IBOutlet weak var sendButton: UIButton!

override func viewDidLoad() {
        super.viewDidLoad()
        self.textField.delegate = self
        if textField.text!.isEmpty {
            sendButton.isUserInteractionEnabled = false
        }
    }

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let inputText = (textField.text! as NSString).replacingCharacters(in: range, with: string)

        if !inputText.isEmpty {
            sendButton.isUserInteractionEnabled = true
        } else {
            sendButton.isUserInteractionEnabled = false
        }
        return true
    }

6 个答案:

答案 0 :(得分:2)

为您的文本字段添加侦听器,您要在 viewDidLoad 方法中为此禁用操作按钮

textField.addTarget(self, action: #selector(actionTextFieldIsEditingChanged), for: UIControlEvents.editingChanged)

并在调用此方法后,检查文本字段是否为空:

@objc func actionTextFieldIsEditingChanged(sender: UITextField) {
     if sender.text.isEmpty {
       sendButton.isUserInteractionEnabled = false
     } else {
       sendButton.isUserInteractionEnabled = true
     }
  }

答案 1 :(得分:1)

viewDidLoad中:

textField.addTarget(self, action: #selector(textChange(_:)), for: UIControl.Event.editingChanged)

操作

@objc func textChange(_ textField: UITextField) {
        if !textField.text!.isEmpty {
            sendButton.isUserInteractionEnabled = true
        } else {
            sendButton.isUserInteractionEnabled = false
        }
    }

答案 2 :(得分:1)

我已经尝试了上述所有解决方案,但是都没有用。我试用了此实现,它是唯一对我有用的实现。

@IBOutlet weak var textField: UITextField!
@IBOutlet weak var sendButton: UIButton!

 override func viewDidLoad() {
     super.viewDidLoad()
     self.textField.delegate = self
     if textField.text!.isEmpty {
        sendButton.isUserInteractionEnabled = false
   }
 }

@IBAction func sendButton(_ pSender: UIButton) {
        generateMessages()
        self.sendButton.isUserInteractionEnabled = !(self.textField.text!.isEmpty)
 }

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let inputText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
        self.sendButton.isUserInteractionEnabled = !inputText.isEmpty
        return true
  }

答案 3 :(得分:0)

您可能会在代码中的某个时候完成

self.textField.text = ""

在此之后,不会再呼叫您的代表。将textField设置为空后,您需要立即将按钮设置为禁用。

答案 4 :(得分:0)

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let inputText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
    sendButton.isUserInteractionEnabled = false
    if !textField.text.isEmpty {
        sendButton.isUserInteractionEnabled = true
    } else {
        sendButton.isUserInteractionEnabled = false
    }
    return true
}

检查上述代码

答案 5 :(得分:0)

下面的代码可能会帮助您,我已经实现了相同的功能。

@IBOutlet weak var textField: UITextField!
@IBOutlet weak var sendButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    self.textField.delegate = self
    if textField.text!.isEmpty {
        sendButton.isUserInteractionEnabled = false
    }

    textField.addTarget(self, action: #selector(ModifiedTextField.textFieldDidChange(_:)), for: UIControlEvents.editingChanged)
}

@objc func textFieldDidChange(_ textField: UITextField) {
    if !textField.isEmpty {
        sendButton.isUserInteractionEnabled = true
    } else {
        sendButton.isUserInteractionEnabled = false
    }
}

或者您可以尝试创建按钮的动作并应用以下条件

@IBAction func sendButtonClicked(sender:UIButton) {
    if !textField.isEmpty{
    // perform your action here
    }

}

如果textField为空,它将不会执行代码