如何在 iOS - Swift 3 中更改两个文本字段为空时禁用 UIBarButton

How to disable a UIBarButton when two textfields are empty while they are changing in iOS - Swift 3

如果两个文本字段为空,我想禁用栏按钮项。 这两个文本字段是 textField 和 textFieldNbrSpans。

对于一个文本字段,我使用了以下代码,它运行良好:

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

    if (newText.length > 0) {

        doneBarButton.isEnabled = (newText.length > 0)
        let tintColor = UIColor(red: 48/255.0, green: 116/255.0, blue: 154/255.0, alpha: 1.0)
        let attrs = [NSForegroundColorAttributeName: tintColor, NSFontAttributeName : UIFont(name: "Cochin-Italic", size: 16)!]
        doneBarButton.setTitleTextAttributes(attrs, for: .normal)

    } else {

        let tintColor = UIColor(red: 48/255.0, green: 116/255.0, blue: 154/255.0, alpha: 0.2)
        let attrs = [NSForegroundColorAttributeName: tintColor, NSFontAttributeName : UIFont(name: "Cochin-Italic", size: 16)!]
        doneBarButton.setTitleTextAttributes(attrs, for: .normal)
        doneBarButton.isEnabled = false

    }

当我试图在上面的代码中添加另一个文本字段时,出现错误:

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

    let oldTextSpan: NSString = textFieldNbrSpans.text! as NSString
    let newTextSpan: NSString = oldTextSpan.replacingCharacters(in: range, with: string) as NSString

    if (newText.length > 0 && newTextSpan.length > 0) {

        doneBarButton.isEnabled = (newText.length > 0 && newTextSpan.length > 0)
        let tintColor = UIColor(red: 48/255.0, green: 116/255.0, blue: 154/255.0, alpha: 1.0)
        let attrs = [NSForegroundColorAttributeName: tintColor, NSFontAttributeName : UIFont(name: "Cochin-Italic", size: 16)!]
        doneBarButton.setTitleTextAttributes(attrs, for: .normal)

    } else {

        let tintColor = UIColor(red: 48/255.0, green: 116/255.0, blue: 154/255.0, alpha: 0.2)
        let attrs = [NSForegroundColorAttributeName: tintColor, NSFontAttributeName : UIFont(name: "Cochin-Italic", size: 16)!]
        doneBarButton.setTitleTextAttributes(attrs, for: .normal)
        doneBarButton.isEnabled = false

    }

有没有办法改进上面的代码,在 2 个文本字段 textfieldtextfieldNbrSpan 为空的情况下禁用条形按钮?

这就是你可以做到的。

  • 你应该有两个 global 变量。
  • 你应该为你的两个 text fields 定义 tags。要做到这一点,select 你的 first textfield 然后转到 attribute inspector 然后在 view section select Tag 作为 0 对于 first text field1 对于 second textfield。和 drag 并为您的文本字段启用 delegates。(假设您知道这些)。
  • 为您的 navigation bar button 创建并 IBOutlet

那么你的view controller should look like. below.

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var mybutton: UIBarButtonItem! // outlet for your button



    var textOne : NSString! = "" // global variable one to hold first text field value
    var textTwo : NSString! = "" // global variable two to hold second text field value

    override func viewDidLoad() {
        super.viewDidLoad()

        mybutton.isEnabled = false // first your button should disabled
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { // delegate method

        if textField.tag == 0 { // get the first text field valued when user change the first text field
            textOne = (textField.text ?? "") as NSString
            textOne = textOne.replacingCharacters(in: range, with: string) as NSString!
        }
        else { // get the second text field value, when user change it
            textTwo = (textField.text ?? "") as NSString
            textTwo = textTwo.replacingCharacters(in: range, with: string) as NSString!
        }


        // check the two variables and do the enable and disable things. do your button styles also here.
        if textOne != "" && textTwo != "" {
            mybutton.isEnabled = true
        }
        else {
            mybutton.isEnabled = false
        }

        return true

    }


}

注意:我没有为按钮实现任何样式,只是想向您展示如何实现。希望这对你有帮助。祝你好运