为TextField创建观察者以启用/禁用按钮时(Swift 3,Xcode 8.x)



我正在尝试创建一个观察者,以便何时指定的文本字段结束编辑,以便禁用/启用一个将按钮转发到新SEGUE。

最后,我想拥有一个只有在TextField已满并完成编辑时才能启用的按钮,这将允许它继续进入下一个VC。

显示的错误是:

 Cannot call value of non-function type '((UITextFieldDelegate) -> (UITextField) -> Bool)?'

这是我的源代码:

import UIKit
class EmailViewController: UIViewController, UITextFieldDelegate {
// Back Button Two
@IBOutlet weak var backButtonTwo: UIButton!
// Email Header
@IBOutlet weak var emailSloganLabel: UILabel!
@IBOutlet weak var emailDescSloganLabel: UILabel!
// Email Form Entries
@IBOutlet weak var emailAddressMiniHeader: UILabel!
@IBOutlet weak var emailAddressTextField: UITextField!
@IBOutlet weak var emailAddressLineBreak: UILabel!
// Bottom Bar 'Next' Navigation
@IBOutlet weak var bottomNextButtonTwo: UIButton!
@IBOutlet weak var bottomNextLineBreakTwo: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()
    emailAddressTextField.delegate = self
    emailAddressTextField.addTarget(self, action:  #selector(UITextFieldDelegate.textFieldShouldEndEditing("textFieldDidChange:")), for: UIControlEvents.editingChanged)
}

internal func textFieldShouldEndEditing(_ emailAddressTextField: UITextField) -> Bool {  if self.emailAddressTextField.text == "" {
    self.bottomNextButtonTwo.isEnabled = false
} else {
    self.bottomNextButtonTwo.isEnabled = true
    }
    return true
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// Hide keyboard when user touches the outside keyboard

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
 }

// Hides keyboard when user pressed the 'Return' Key
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    emailAddressTextField.resignFirstResponder()
    return (true)
}
}

谢谢您的任何帮助,我非常感谢。

您应该删除addTarget,因为它将与UITextFieldDelegatetextFieldShouldEndEditing冲突,并应直接使用它以知道何时textfield完成编辑

最新更新