You can do it in the following way :
protocol ViewControllerBDelegate: class {
func changeColor(color : UIColor)
}
class ViewControllerB: UIViewController {
weak var delegate : ViewControllerBDelegate?
@IBAction func changeColorInViewController(sender: UIButton) {
// send the message to change the color in A regarding the color
sender.tag == 0 ? delegate?.changeColor(UIColor.redColor()) :
delegate?.changeColor(UIColor.greenColor())
}
}
The above ViewController
is the ViewControllerB
in which you want to change the color for the ViewControllerA
.
Then you can implement the ViewControllerA
in the following way :
class ViewControllerA: UIViewController , ViewControllerBDelegate {
var viewControllerB : ViewControllerB!
// In this method you receive the notification when the button in B is tapped
func changeColor(color: UIColor) {
self.view.backgroundColor = color
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var dest = segue.destinationViewController as! ViewControllerB
self.viewControllerB = dest // instantiate the reference
self.viewControllerB.delegate = self // set the delegate for B
}
}
Two important things :
- I set the reference for the
ViewControllerB
in the prepareForSegue
because you have a button to open the ViewControllerB
, but in you present it manually you can change it as do you want.
- I only implemented an
action
for the two buttons in the ViewControllerB
, and I assigned a tag
for each (you can do it in Interface Builder or in code) to recognize it and send the color regarding the button pressed, but you can do it separately if you want.
I hope this help you.