I have managed to get the text field validation working which doesn't allow the add button to be active unless there is text in the textbox field.
However, now when I press the add button the app crashes.
I have done some research and it looks like its because of the:
calarieSubmit.addTarget(self, action: #selector(calarieSubmitDidChange), for: .editingChanged)
which is then placing the code elsewhere in the memory and not accessible by the add button. If I move the #selector code out of the view did load and into the top of the controller, the calariesSubmitDidChange textbox isn't seen.
Does anyone know where the code needs to be placed to stop the crashing or if I need to rewrite the code?
Thanks!
**Update - Sorry for not copying the code into this, I have now done this below:
import UIKitimport Firebaseclass ItemToAddViewController: UIViewController { var finalName = ""override func viewDidLoad() { super.viewDidLoad() addButton.isEnabled = false show.text = "\(finalName)" calarieSubmit.addTarget(self, action: #selector(calarieSubmitDidChange), for: .editingChanged)}@IBOutlet weak var button: UIButton!@IBOutlet weak var show: UILabel!@IBOutlet weak var calarieSubmit: UITextField!@IBAction func calarieSubmitDidChange(_ sender: Any) { //Validate Input Button // print("\(calarieSubmit.text!)") if calarieSubmit.text!.isEmpty { addButton.isEnabled = false } else { addButton.isEnabled = true } }@IBOutlet weak var addButton: UIButton! @IBAction func addButton(_ sender: Any) { let input = calarieSubmit.text //Send New Item to Database var ref: DatabaseReference! ref = Database.database().reference() ref.child("items").updateChildValues((["\(finalName)": "\(input!)"])) let alert = UIAlertController(title: "Added!", message: "Item now added!", preferredStyle: .alert) alert.addAction(UIAlertAction(title: NSLocalizedString("Return", comment: "Default action"), style: .default, handler: { (action) -> Void in _ = self.navigationController?.popViewController(animated: true) })) self.present(alert, animated: true, completion: nil) //Move in once added or show error } }
I tried everything below with adding the @objec in front of the function but no luck. The app crashes everytime the add button is pressed. However the data is still passed to the database and then crashes on the UIAlertController.