title: Sending Images to Several Instances of a ViewController in Swift
body: <p>I download several "events" from CloudKit in my program. Each one contains a picture, title, and location. The second two are used right away and there is no problem. The image is only seen if the user clicks on the info button on the annotation that shows the picture using a separate view controller.  I am having trouble getting the images to be passed to the ViewController before it is used while also keeping each event's picture separate.</p>
  
  <p>Here is where each record is queried from CloudKit and put into a for loop that send then to the method below:</p>
  
  <pre><code>func loadEvent(_ completion: @escaping (_ error:NSError?, _ records:[CKRecord]?) -&gt; Void)
      {
          //...record is downloaded from cloudkit
  
                  for record in records
                  {
                      if let asset = record["Picture"] as? CKAsset,
                          let data = NSData(contentsOf: asset.fileURL),
                          let image1 = UIImage(data: data as Data)
                      {
                          self.drawEvents(record["LocationF"] as! CLLocation, title1: record["StringF"] as! String, pic1: image1)
                      }
                  }
              }
  </code></pre>
  
  <p>Here is where the variables are assigned and used to create the point annotation (it is a custom one that includes an image):</p>
  
  <pre><code>func drawEvents(_ loc: CLLocation, title1: String, pic1: UIImage)
      {
          mapView.delegate = self
          let center = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
          let lat: CLLocationDegrees = center.latitude
          let long: CLLocationDegrees = center.longitude
          self.pointAnnotation1 = CustomPointAnnotation()
          self.pointAnnotation1.imageName = pic1
          self.pointAnnotation1.title = title1
          self.pointAnnotation1.subtitle = "Event"
          self.pointAnnotation1.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
          self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation1, reuseIdentifier: nil)
          self.mapView.addAnnotation(self.pinAnnotationView.annotation!)
      }
  </code></pre>
  
  <p>Here is the function that makes EventPage appear when the info button on an MKMapAnnotation is clicked:</p>
  
  <pre><code>    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
  
              let eventPageViewController:EventPageViewController = storyboard?.instantiateViewController(withIdentifier: "EventPage") as! EventPageViewController
  
              self.present(eventPageViewController, animated: true, completion: nil)
          }
  </code></pre>
  
  <p>Here is the ViewController for EventPage:</p>
  
  <pre><code>class EventPageViewController: UIViewController {
  
      @IBOutlet weak var eventPic: UIImageView!
  
      var photo: UIImage!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          let firstViewController:FirstViewController = storyboard?.instantiateViewController(withIdentifier: "Home") as! FirstViewController
  
          photo = firstViewController.pointAnnotation1.imageName 
          //photo is nil
  
          eventPic.image = photo
     }
  }
  </code></pre>