Change all view controllers background from one view controller

I am a beginner to programming and I am trying to create an app which has like 5 view controllers. One of the 5 view controllers is the settings view controller that has 4 buttons where I am allowing the user to pick a new app background. How do I change the background of all the 5 view controllers to the background the user has picked. Basically I am trying to change the imageView of all view controllers to the image that user picks in the settings view controller. Can anyone out there help me?

1.Declare global variable(right under import UIKit) like this
var imageName = “”
2.Create IBoutlet in each ViewController for it’s imageview, name them imageview .
3.In each ViewController in viewdidload write
imageview.image = UIImage(named: imageName)
4. in your button that you want to change set the name of the image
imageName = “the name of your image”

1 Like

Thanks for getting back. The first line that you are asking me to type “var imageName under import UIKit” will reside in which view controller? Do you mean type this inside settings view controller?

It is global variable. That means it is visible to all viewcontrollers. It does not matter in what view controller you type.

1 Like

Ok. Thanks. Let me give a try. I will get back to you shortly. Thanks again.

You are welcome ) If you have any questions concerning this let me know.

1 Like

I think I am doing something wrong. I did do what you said but it did not work. Please bear with me so I can explain you below.

See the attached screen shot. My settings view controller has five buttons. Each buttons sets a different image for the view controller background. For example, if I press the blue button it will set the background image of all 4 view controllers to blue. I have the images in my assets. I am also trying to use User Defaults to save the user selection. This way the app remembers the user settings. When the app is installed and user opens for the first time all view controllers will have the first image. I have assigned the button tag with numbers from 1 to 5. Each time the user taps on a different button to change the background I am saving the button tag value in a variable and using it for updating the back ground images. But still somewhere something is not right. Sorry I don’t want to take up too much of your time. Please let me know how you can further help me. If not it is totally fine I can understand that you are busy too. Thank you helping me so far.

Can you paste your code, where the global variable is declared, also the Settings code. What does it not do - does not change all backgrounds, or does not save the choice for user?
For me it works. Here is my code for 3 view controllers( one of them is the settings).
1.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var imageview: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    imageview.image = UIImage(named: myimage)

}

}

  1. second viewcontroller

import UIKit

class SecondViewController: UIViewController {

@IBOutlet weak var imageview: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    imageview.image = UIImage(named: myimage)
}

}

3.settings view controller

import UIKit
var myimage = “”

class Settings: UIViewController {

@IBOutlet weak var imageview: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    imageview.image = UIImage(named: myimage)

}

@IBAction func changebackg(_ sender: UIButton) {
    myimage = "04"
}

}


I have an image named 04 in my assets catalog.

1 Like

do you have an email. I want to send you my entire project file.