Example app → left: base screen, right: selection screen / When side is chosen, the base screen shows a different screen depending on the user selection
Q. How to know which is the intern and which is the boss?
A. Which one "knows" all the information? → Boss / Which screen is just "acting" based on the information? → Intern
Steps:
First, define a protocol in the boss screen. It will be like a list of commands that you will give to the intern.
→ Think of it as a command list for our intern
//SelectionScreen.swift
protocol SideSelectionDelegate{
func didTapChoice(image: UIImage, name: String, color: UIColor)
}
class SelectionScreen: UIViewController{
var selectionDelegate: SideSelectionDelegate!
//Our intern's name is selectionDelegate!
//Notice we are not setting this to anything. We are going to set its actual value in the base screen
//So, we named our intern, but we didn't actually "make" it.
override func viewDidLoad(){
super.viewDidLoad()
}
@IBAction func imperialButtonTapped(_ sender: UIButton){
dismiss(animated: true, completion: nil)
}
@IBAction func rebelButtonTapped(_ sender: UIButton){
dismiss(animated: true, completion: nil)
}
}
@IBAction func imperialButtonTapped(_ sender: UIButton){
selectionDelegate.didTapChoice(image: UIImage(named: "Vadar")!, name: "Darth Vader" , color: .red)
//Boss: Hey intern, the imperialButton has been tapped, hold these 3 parameters (info) for me
//so you can do stuff
dismiss(animated: true, completion: nil)
}
@IBAction func rebelButtonTapped(_ sender: UIButton){
selectionDelegate.didTapChoice(image: UIImage(named: "Luke")!, name: "Luke Skywalker" , color: .cyan)
dismiss(animated: true, completion: nil)
}
class BaseScreen: UIViewController{
@IBOutlet var mainImageView: UIImageView!
@IBOutlet var chooseButton: UIButton!
@IBOutlet var nameLabel: UILabel!
@IBAction func chooseButton(_ sender: UIButton){
let selectionVC = storyboard?.instantiateViewController(withIdentifier: "SelectionScreen") as!
SelectionScreen
selectionVC.selectionDelegate = self //I would like to be your intern
present(selectionVC, animated: true, completion: nil)
}
}
→ In the base screen (intern), it just gets information from the boss, and shows it.
extension BaseScreen: SideSelectionDelegate{
//This function gets called automatically when either of the 2 buttons from the boss screen
//gets tapped. So you don't have to manually call the below function in the intern screen
//This is where the intern actually does the work after the boss passes the info needed
func didTapChoice(image: UIImage, name: String, color: UIColor){
mainImageView.image = image //The info is passed, so we assign the value from the parameter
nameLabel.text = name
view.backgroundColor = color
}
}
→ Our intern just performs the job, setting the screen value, after the boss passes the information
class BaseScreen: UIViewController{
@IBOutlet var mainImageView: UIImageView!
@IBOutlet var chooseButton: UIButton!
@IBOutlet var nameLabel: UILabel!
@IBAction func chooseButton(_ sender: UIButton){
let selectionVC = storyboard?.instantiateViewController(withIdentifier: "SelectionScreen") as!
SelectionScreen
selectionVC.selectionDelegate = self
present(selectionVC, animated: true, completion: nil)
}
}
extension BaseScreen: SideSelectionDelegate{
func didTapChoice(image: UIImage, name: String, color: UIColor){
mainImageView.image = image
nameLabel.text = name
view.backgroundColor = color
}
}