Bastek Bastek - 7 months ago 14
Swift Question

Delegation between two ViewControllers without segues

I am learning the concept of delegation and I am stuck in my project. I believe the solution is simple, but as this is something new for me I have no idea what is wrong.

Project concept is simple:

There are two views in the application. In the first view you press the button "change the color" and as a result second view appears. In the second view there are three text fields where user puts numbers respectively for R G and B values in RGB color. When the button is tapped, second view disappears and the first view's background should be changed with color based on the user's input. I assume that the user put correct numbers, therefore at this moment I use forced unwrapping for those values.

At this moment views appear correctly, but the background color of the first view does not change and I have no idea why.

Beneath is the code for two view controllers. I will appreciate any hints.

First VC: ViewController.swift

class ViewController: UIViewController, ColorChangeDelegate {

let secondStoryboard = UIStoryboard(name: "Second", bundle: nil).instantiateViewControllerWithIdentifier("SecondViewController") as UIViewController
var secondVC = SecondViewController()


@IBAction func changeColourButtonTapped(sender: UIButton) {
presentViewController(secondStoryboard, animated: true, completion: nil)
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
secondVC.colorDelegate = self
}

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

func didChangeColor(controller: SecondViewController, color: UIColor) {
self.view.backgroundColor = color
}


}

and second VC: SecondViewController.swift

protocol ColorChangeDelegate {
func didChangeColor(controller: SecondViewController, color: UIColor)
}

class SecondViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var myRTextField: UITextField!
@IBOutlet weak var myGTextField: UITextField!
@IBOutlet weak var myBTextField: UITextField!

var colorDelegate : ColorChangeDelegate?

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
myRTextField.delegate = self
myGTextField.delegate = self
myBTextField.delegate = self
}

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

func textFieldDidBeginEditing(textField: UITextField) {
textField.becomeFirstResponder()
}

@IBAction func goButtonTapped(sender: UIButton) {
let r = Int(myRTextField.text!)!
let g = Int(myGTextField.text!)!
let b = Int(myBTextField.text!)!
let color = UIColor(red: CGFloat(r), green: CGFloat(g), blue: CGFloat(b), alpha: 1.0)
self.view.endEditing(true)
colorDelegate?.didChangeColor(self, color: color)
presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
}

Answer

You are assigning your delegate to secondVC but the view controller that is actually being presented is in secondStoryboard, so you should set the colorDelegate property of secondStoryboard.