Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift3 当两个文本字段在iOS-Swift 3中更改时为空时,如何禁用UIBarButton_Swift3_Uitextfielddelegate - Fatal编程技术网

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

Swift3 当两个文本字段在iOS-Swift 3中更改时为空时,如何禁用UIBarButton,swift3,uitextfielddelegate,Swift3,Uitextfielddelegate,如果两个文本字段为空,我想禁用bar按钮项。 这两个文本字段是textField和textField 对于一个textfield,我使用了以下代码,效果很好: func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let oldText: NSString = textField

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

对于一个textfield,我使用了以下代码,效果很好:

    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

    }

如果两个文本字段textfield&textfieldnbspan为空,是否有办法通过禁用条形按钮来改进上述代码

这就是你可以做到的

您应该有两个全局变量。 您应该为两个文本字段定义标记。为此,请选择第一个文本字段并转到属性检查器,然后在“视图”部分下,选择标记0作为第一个文本字段,选择标记1作为第二个文本字段。拖动并启用文本字段的代理。假设您知道这些。 为导航栏按钮创建并安装插座。 那么视图控制器应该是这样的。下面


注意:我没有为button实现任何样式,只是想向您演示如何实现。希望这对你有帮助。祝你好运你犯了什么错误
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

    }


}