swift - Add an image inside a scrollView from another class? -


i m trying add image subview scrollview outlet exist in class

i did create object within external class parent class scrollview outlet exists

var imageclassname = imageviewcontroller() 

but when ever try add an image scroll view

imageclassname.scrollview.addsubview(imageview) 

i following error "fatal error: unexpectedly found nil while unwrapping optional value"

i tried adding optional @ end still same error

imageclassname.scrollview?.addsubview(imageview) 

any clues?

this (probably) common misconception. when started out, had problem too.

imagine being, rather ios developer, miracle real-world product designer. designed blueprints magical box has user put in photos , scroll through it(kind of magical collage). show 100 people in focus group , 95 that. send blueprint manufacturing company produces 100's of identical products. similar scenario.

your imageviewcontroller.swift blueprints. making new instance of saying: "put image magicbox's blueprints". ios has no idea uiviewcontroller class imageviewcontroller put picture in, since can reuse infinitely many times. need specify which 1 want(even though have one)

therefore, must:

  1. go storyboard
  2. select target imageviewcontroller
  3. click on third icon left on right toolbar
  4. enter id(like "imagevc1") in "storyboard id" section
  5. replace var imageclassname = imageviewcontroller() with:

    var imageclassname: imageviewcontroller = self.storyboard!.instantiateviewcontrollerwithidentifier("id entered in step 4") as! imageviewcontroller

and you're good!


Comments