<file_sep># Protocols
Pick and choose an option from a second controller and reflecting the changes in the first using protocols and delegate methods.


<a href="https://imgflip.com/gif/2507ws"><img src="https://i.imgflip.com/2507ws.gif" title="made at imgflip.com"/></a>
<file_sep>//
//  ChoosePlayerVC.swift
//  CustomDelegatesProtocols
//
//  Created by Apple on 21/02/18.
//  Copyright © 2018 Vignesh. All rights reserved.
//

import UIKit

protocol myPlayerDelegate {
     func didTapChoice(image: UIImage, name: String, color: UIColor)
}

class ChoosePlayerVC: UIViewController {

    // delegate
    var playerDelegate : myPlayerDelegate!
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
    func dismissAction() {
    dismiss(animated: true, completion: nil)
    }

    @IBAction func messiPicked(_ sender: Any) {
        playerDelegate.didTapChoice(image: UIImage(named: "mes")!, name: "messi", color: UIColor.orange)
        dismissAction()
    }
    
    @IBAction func ronaldoPicked(_ sender: Any) {
         playerDelegate.didTapChoice(image: UIImage(named: "ron")!, name: "Ronaldo", color: UIColor.magenta)
        dismissAction()
    }
    
}
<file_sep>//
//  ViewController.swift
//  CustomDelegatesProtocols
//
//  Created by Apple on 21/02/18.
//  Copyright © 2018 Vignesh. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    // Outlets:
    @IBOutlet weak var playerImageView: UIImageView!
    @IBOutlet weak var playerNameLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }

    @IBAction func pickWinnerPressed(_ sender: Any) {
        let pickPlayerVC = storyboard?.instantiateViewController(withIdentifier: "pickPlayerVC") as! ChoosePlayerVC
        pickPlayerVC.playerDelegate = self as myPlayerDelegate
        present(pickPlayerVC, animated: true, completion: nil)
    }
}

extension ViewController: myPlayerDelegate {
    func didTapChoice(image: UIImage, name: String, color: UIColor) {
        playerImageView.image = image
        playerNameLabel.text = "Hey, You picked \(name),Congrats!"
        view.backgroundColor = color
    }
}
