0

I am doing some excercises now. For example I create two view controllers in storybard and I want to change color for example from VC2 in VC1 by clicking button. In this situation delegate is needed or is other way to do it?

motivated
  • 53
  • 8

1 Answers1

0

Normally, yes. But that depends . You can use NotificationCenter, you can use "prepare" function. The delegate is the most common option here. In your case:

protocol ChangeButtonColorDelegate {
    func changeButtonColor() 
}

class VC1 : ChangeButtonColorDelegate... {
    func changeButtonColor() {
        /* change button color here */
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "goto_vc2") {
            (segue.dest as! VC2).delegate = self 
        }
    }
}

class VC2 : ... {
    var delegate : ChangeButtonColorDelegate!
}
chr0x
  • 1,151
  • 3
  • 15
  • 25