0

So I have an image view in one viewcontroller.swift file that generates a random background image every time the a question is asked (it's a quiz app). When the user get's the answer right I want the random background image produced from the first view controller when the question is asked to be the same background image for the next view controller that displays when the answer is answered correctly. How do I make sure the both background image views displays the same random image every time a question is correctly answered? Here is a snippet of my code that deals with the background image.

class ViewController : UIViewController

@IBOutlet weak var backgroundImage: UIImageView!

let backgroundImages = //array containing all the images


//random image generator function
func randomImage() {
    index = Int(arc4random_uniform(UInt32(backgroundImages.count)))
    backgroundImage.image = backgroundImages[index]
}

Now if I wanted an image view to always display the same background images produced by the randomImage function in a different class (class SecondViewController: UIViewController) for example what would be the best way to get the data from the original view controller? (If you click the right answer button according to the question it proceeds to a new "right answer" view controller that I want to have the same background image as the previous view controller).

Captain Code
  • 277
  • 1
  • 3
  • 13
  • 2
    You need to be just a bit ore specific, as there are *several* ways to do this. The most important detail you haven't answered is how are the two view controllers connected? But wait - there's more. What have you tried? That's great that you've shown some kind of code - but you really haven't shown **any** that matters! –  Jul 05 '17 at 02:17
  • the view controllers are connected as so: the first view controller is the base view controller where the question is displayed and shown with its answer choices and if you click the button that has the right answer choice then I'm trying to get it to where it progresses to another view controller (the view controller that displays when you select the right answer) to have the same background image displayed as the base view controller – Captain Code Jul 05 '17 at 03:25

4 Answers4

0

First you have to store the background image index that you will set in func randomImage(). So you have to declare a class variable

class ViewController : UIViewController

@IBOutlet weak var backgroundImage: UIImageView!
let randomIndex:Int!
let backgroundImages = //array containing all the images


//random image generator function 
func randomImage() {
    // So now you can pass this value to next viewController.
    randomIndex = Int(arc4random_uniform(UInt32(backgroundImages.count)))
    backgroundImage.image = backgroundImages[randomIndex]

}
vhong
  • 594
  • 4
  • 27
0

you can use storyboard segue and share image with prepare statement else you can make instance of second vc and then you need to initialize one image view than you need to set that image view from 1st VC and push that vc.

anshul king
  • 558
  • 4
  • 17
0

Make the subclass of UIImageView and use it.

0

You can save the image as a png in the user document folder and reload it as needed. See my recent answer to an unrelated question:

Saving an image to User Documents folder

Mozahler
  • 4,958
  • 6
  • 36
  • 56