So The community has been a great helping me along with a school project. However I've hit a snag. I'm building a simple Kitchen timer app thats a SVC with a tab view. The first page allows the user to set the timer, then the second is a basic settings page that allows the user to trigger a random background color I made to fulfill the requirements of the assignment. Everything is working great say for one part. Once the user triggers a random color to be used as a background, I wanted that background color to also be applied to the other views. I went with the advice I sought out here: Change viewController BG color based off another view controller and used singleton. However when I navigate from the settings view, to the timer view nothing happens. Now I added self.view.backgroundColor = Settings.sharedInstance.backgroundColor
to my viewDidLoad()
like so:
`override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = Settings.sharedInstance.backgroundColor
print(Settings.sharedInstance.backgroundColor)
}
` Nothing seems to happen. However! as a test, I added the same block to :
@IBAction func pickerStartButtonChnage(_ sender: UIButton) {
self.view.backgroundColor = Settings.sharedInstance.backgroundColor
print(Settings.sharedInstance.backgroundColor)
if !cron.isValid{ // prevent more than one time on the thread
nameIt.text = cronStr(cronCount)
cron = Timer.scheduledTimer(timeInterval: timeInterval,
target: self,
selector: #selector(TimerViewController.timerDidEnd(_:)),
userInfo: "is done!",
repeats: true) //repeating timer in the second iteration
}
}
It works!
My question is why is the background not being triggered in the viewDidLoad()
, when it works fine when the button is triggered? I appreciate any advice in advance, thank you.
Change the color in viewWillAppear()
instead. viewDidLoad()
happens when the view is first loaded into memory, but viewWillAppear()
gets called every time it will appear.
override func viewWillAppear() {
super.viewWillAppear()
self.view.backgroundColor = Settings.sharedInstance.backgroundColor
}