enable or unhide a button in swift 3

1

I have several buttons inside a view controller, but there is one of them that I want to be disabled if in a textfield there is no written value.

If any value is written, it must be re-enabled.

import UIKit

class AreaOfAnIsoscelesTriangleViewController: UIViewController {

    @IBOutlet var texfieldA: UITextField!
    @IBOutlet var textfieldB: UITextField!

    @IBOutlet var label1: UILabel!
    @IBOutlet var label2: UILabel!
    @IBOutlet var label3: UILabel!
    @IBOutlet var label4: UILabel!
    @IBOutlet var label5: UILabel!
    @IBOutlet var label6: UILabel!
    @IBOutlet var label7: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

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

    @IBAction func calculateButton(_ sender: UIButton) {

        textfieldB.resignFirstResponder()
        texfieldA.resignFirstResponder()


        let numbersquareA = Double(texfieldA.text!)!
        let resultSquareA = pow(numbersquareA, 2)

        let numbersquare  = Double(textfieldB.text!)!
        let resultSquare = pow(numbersquare, 2)

        let variableA = Double(texfieldA.text!)!
        let variableB = Double(textfieldB.text!)!


        label1.text = "b * √a^2 - b^2 /4 /2"
        label2.text = "\(variableB) * √\(variableA)^2 - \(variableB)^2 /4 /2"
        label3.text = "\(variableB) * √\(resultSquareA) - \(resultSquare) /4 /2"

        let operation1 = resultSquare / 4
        let operation2 = resultSquareA - operation1

        label4.text = "\(variableB) * √\(resultSquareA) - \(operation1)  /2"
        label5.text = "\(variableB) * √\(operation2)/2"

        let operation3 =  sqrt(operation2)
        let operation3final = String(format: "%.2f", operation3)

        label5.text = "\(variableB) * \(operation3final)/2"

        let operation3intValue = Double(operation3final)

        let operation4 = variableB * operation3intValue!
        let operation5 = operation4/2



        label6.text = "\(operation4)/2"
        label7.text = "\(operation5)"


    }





    @IBAction func bottonstep(_ sender: UIButton) {



        let variableA = Double(texfieldA.text!)!
        let variableB = Double(textfieldB.text!)!

        let numbersquareA = Double(texfieldA.text!)!

        let numbersquare  = Double(textfieldB.text!)!
        let resultSquareA = pow(numbersquareA, 2)
        let resultSquare = pow(numbersquare, 2)


        let alertControler : UIAlertController = UIAlertController (title: "The steps to follow are those :", message: "Step 1 - (b * √a^2 - b^2 /4 /2).Replace the variable of the equation with its corresponding values in this case would be \(variableB) * √\(variableA)^2 - \(variableB)^2 /4 /2 \n Step 2: To raise numbers according to their exponents \(variableB) * √\(resultSquareA) - \(resultSquare) /4 /2" , preferredStyle: .actionSheet)

        let okAction :UIAlertAction = UIAlertAction(title: "ok", style: .default, handler: nil)

        alertControler.addAction(okAction)

        present(alertControler , animated: true, completion: nil)

    }

The button that I need to be disabled if a value has not been entered in the text field is bottonstep .

How would the method be? Would I have to put it as an outlet?

    
asked by Liantony Pozo 02.08.2017 в 01:16
source

2 answers

2

As you say, you have to create an outlet for the button if you want to access it from code. The function would be something like this:

func textFieldDidChange(textField: UITextField) {
        if texField.text?.isEmpty{
            boton.isEnabled = false
        }else{
            boton.isEnabled = true
        }
}

And in the viewDidLoad you tell the corresponding textField to call the function:

comentarioTF.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: UIControlEvents.editingChanged)

If the textField is going to be empty when you start, leave the button disabled in viewDidLoad or from the Accessibility section in the Identity Inspector

If you want more than one textField to make the call but do different things assign different tags and check them in the function

    
answered by 02.08.2017 / 08:18
source
2

First you have to add the UITextFieldDelegate protocol and inside the function:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
  if yourTextfield == textField{
     buttonToDisable.isEnabled = (string != "" && textField.text != "")
  }
  return true

}

I recommend that you adopt the protocol in an extension of your ViewController.

    
answered by 02.08.2017 в 14:48