Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to add an @IBAction from the background view to ViewController.swift

ID: 3747494 • Letter: I

Question

I am trying to add an @IBAction from the background view to ViewController.swift that responds to Touch Down events. My IBAction will make a sequence black -> white -> light gray -> dark gray -> red -> black. This sequence should repeat indefinitely as the user taps the screen. For the sake of me, I can't figure out why its not working..

class ViewController: UIViewController {

  override func viewDidLoad() {

  super.viewDidLoad()

  self.changeBackground(self)

  self.view.backgroundColor = UIColor.black

  // Do any additional setup after loading the view, typically from a nib.

  }

  

  @IBAction func changeBackground(_ sender: Any) {

  

  self.view.backgroundColor = UIColor.black

  

  if (self.view.backgroundColor  == UIColor.black){

  self.view.backgroundColor = UIColor.white

  }

  

  if (self.view.backgroundColor  == UIColor.white){

  self.view.backgroundColor = UIColor.lightGray

  }

  

  if (self.view.backgroundColor  == UIColor.lightGray){

  self.view.backgroundColor = UIColor.darkGray

  }

  

  if (self.view.backgroundColor  == UIColor.darkGray){

  self.view.backgroundColor = UIColor.red

  }

  

  if (self.view.backgroundColor  == UIColor.red){

  self.view.backgroundColor = UIColor.black

  }

  

  }

Explanation / Answer

when ever changeBackGround function is called........

in changeBackGround function in first line YOU HAVE SET VIEW.BACKGROUNDCOLOR = BLACK

@IBAction func changeBackground(_ sender: Any) {

self.view.backgroundColor = UIColor.black //you are setting backgroundColor to black

  

//so the only the first if condition executed every time

//so colors will be only black and white

if (self.view.backgroundColor == UIColor.black){

self.view.backgroundColor = UIColor.white

}

;

;

;;

;;

;

;;

}

so "every Time only first IF-BLOCK" will be executed and ONLY WHITE AND BLACK COLURS are repeated.

solution == so REMOVE the SELF.VIEW.BACKGROUNDCOLOR = UICOLOR.BLACK in changeBackGround function

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote