How to know the status of an UIButton from another view?

0

I have a view where the status of a UIButton is changed from true to false several times in several code parts. In another view I have to implement a method that according to the UIButton state of this other view does one thing or another. Is it possible to use something similar to "didset" so that depending on the state of this UIButton call a function? Should I use it with a protocol?

    
asked by Popularfan 09.07.2018 в 20:03
source

1 answer

2

With the observable property "didset" you can as its name indicates to monitor the change of value of a property in specific, even if the new value is the same as the current value of the property. For example:

class MyButton : UIButton {

    override var isSelected: Bool {
       didSet {
        print("Cambio de \(oldValue) a \(isSelected)")
       }
    }
}

let button = MyButton()
button.isSelected = true //se visualizaría en consola la impresión del mensaje

However, with this you would not be communicating a controller with another for this there may be different ways. One of them, as you mentioned, is to use a protocol.

* I did not understand very well how you have the communication from your controllers but I give you a simple example.

protocol CambioDelegate {
   func cambioEstadoBoton(boton:UIButton)
}

class ButtonController: UIViewController {
    weak var delegate: ViewController?
    @IBOutlet weak var boton: UIButton!

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

    @IBAction func clicEnBoton(_ sender: Any) {
       if boton.isSelected == true {
          boton.isSelected = false
       } else {
          boton.isSelected = true
          delegate?.cambioEstadoBoton(boton: boton)
       }
    }
}

class ViewController: UIViewController, CambioDelegate {
    override func viewDidLoad() {
       super.viewDidLoad()
    }

    func cambioEstadoBoton(boton: UIButton) {
       print("Entra a cambio de estado")
    }
}

Where the delegate reference could be given for example if there is a segue in your storyboard that is executed to show "ButtonController"

override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) 
{
   if segue.identifier == "ButtonController" {
       let vc = segue.destination as! ButtonController
       vc.delegate = self
   }
}
    
answered by 10.07.2018 / 01:32
source