0

I created a custom class UIView so that I can a portion of an image from my viewController class. I created the UIView that I would like to to magnify in the viewController class and named its @IBOutlet property xRaySuperView: UIView! I embeded the Magnifier: UIView custom class view in this view using the storyboard.

I then instantiated a viewController object in the custom Magnifier class. However, when I try to transform the layer of the xRaySuperView and render it in context, the build fails and states 'unexpected nil while unwrapping optional value'.

It appears that my xRaySuperView is not passing it layer value to the Magnifier class.? (IT is th

It is the last line creating the error during build.

import UIKit
import QuartzCore

class MagnifierView: UIView {


    var viewController: ViewController!


    //  First Step:  Initialize the UIView
    required init?(coder aDecoder: NSCoder) {

        //  Create instance of ViewController Class
        let viewController = ViewController()
        super.init(coder: aDecoder)


        //  Makes the circle-shape outline.
        self.layer.borderColor = UIColor.blue.cgColor
        self.layer.borderWidth = 4
        self.layer.cornerRadius = 65 //1/2 the width of the frame
        self.layer.masksToBounds = true

    }

    //  Perform transforms on the xray view
    override func draw(_ rect: CGRect) {
        let xrayContext = UIGraphicsGetCurrentContext()
        xrayContext?.translateBy(x: self.frame.size.width / 2, y: self.frame.size.height / 2)
        xrayContext?.scaleBy(x: 1.5, y: 1.5)
        self.viewController.xRaySuperView.layer.render(in: xrayContext!)
    }
}
Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
beckp
  • 127
  • 11

1 Answers1

3

When you use ViewController(), you're just creating an object, not filling in its outlets (unless you have init code in ViewController to do that). You either need to instantiate a ViewController from your storyboard or .xib file or -- more likely -- pass an existing instance to this class.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • Sorry for the simple question, but I spent the last 2 hours trying to learn how to pass an existing instance to the Magnifier custom class (I am new to coding). When I search for how to do this in Swift, I am only finding answers on how to pass data between view controllers using segues. I reviewed the Swift 3 Apple Programming Guide but cannot find what I need. To pass, does this require some type of protocol 'delegate' method? Thank you for your patience. – beckp Oct 04 '16 at 17:03
  • Are you loading this view as part of a `ViewController` from a storyboard? If you are, make the `viewController` variable an outlet and connect it to the owning `ViewController` within the visual editor. – Phillip Mills Oct 04 '16 at 17:11
  • It WORKED!!! Thank you!!! I was connecting all the outlets to the original ViewController. I did not create the outlet in the custom class Magnifying UIView and connect it back up to the original ViewController. I really appreciate your help, I have spent an embaressing number of days trying to figure this out before posting today. – beckp Oct 04 '16 at 17:51