0

I uses storyboard to build the layout. I am trying to access func in firstViewController from secondViewController but when I use the following code to access it I will always get "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" no matter what function or outlet I have tried to access I always get the same error. Did I miss anything or am I doing it wrongly?

done is located in secondViewController

func done(){
    let vc =  firstViewController()
    vc.drawCircle(locationX: 0, locationY: 0)
}

drawCircle is located in firstViewController

func drawCircle(locationX:CGFloat, locationY: CGFloat) {
    let path = UIBezierPath(roundedRect: CGRect(x: locationX, y: locationY, width: radius, height: radius), cornerRadius: 50).cgPath
    combinePath.addPath(path)
    layer.path = combinePath

    if thickness>0{
        layer.strokeColor = UIColor(red: rColor, green: gColor, blue: bColor, alpha: 1).cgColor
        layer.fillColor = UIColor.clear.cgColor

        layer.lineWidth = thickness
    }
    imageView.layer.addSublayer(layer)

}
K Y
  • 39
  • 7

3 Answers3

1

Default constructor does not create UI component from storyboard (IBOutlet, IBAction,..)

Huy Nguyễn
  • 72
  • 1
  • 3
1

If you are using storyboards, give an identifier to view controller and use this:

if let firstVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "firstViewController") as? firstViewController {
    firstVC.drawCircle(locationX: 0, locationY: 0)
}
Mahmut Acar
  • 713
  • 2
  • 7
  • 26
  • Hi I have just tried this but it just give me "Thread 1: signal SIGABRT" error and in the log it says " Storyboard () doesn't contain a view controller with identifier 'firstViewController" – K Y Sep 12 '18 at 07:56
  • Did you give an identifier to firstViewController in Identity Inspector? – Mahmut Acar Sep 12 '18 at 07:58
  • there isn't a identifier for firstViewController. – K Y Sep 12 '18 at 08:11
  • I am not sure this will help but before firstViewController I actually have another viewController called viewController. – K Y Sep 12 '18 at 08:12
  • You must give an identifier. If you don't know how to do it, this answer helps : https://stackoverflow.com/a/45723662/5130481 – Mahmut Acar Sep 12 '18 at 08:22
  • I have added the identifier but now it gives me the same error as before where it says "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" – K Y Sep 12 '18 at 08:31
  • I think this isn't the cause of the error. You might missed something in your code. – Mahmut Acar Sep 12 '18 at 08:43
  • Noted. Thank you very much for your help. – K Y Sep 12 '18 at 08:46
0

The problem is due to the reference to imageView in function drawCircle because it means that in order for it to run, the firstViewController must be fully initialised and on-screen for the imageView to be "hooked up" to the User Interface.

Are both the firstViewController and secondViewController supposed to be on-screen at the same time?

To solve your problem you should drag two Container Views into your storyboard. The Custom Class (in the identity inspector) for each Container View should be firstViewController and secondViewController respectively.

Then when you are on-screen, both controllers will be initialised, and imageView will be hooked up properly (the Implicitly Unwrapped Optional will be non-nil). This means you will then be able to safely call the drawCircle method.

Faisal Memon
  • 2,711
  • 16
  • 30
  • firstViewController and secondViewController are not supposed to be on-screen at the same time – K Y Sep 12 '18 at 08:33
  • OK. In that case you need to update a value in a shared data structure (a model object) with the location (0,0), and a Bool shouldDrawCircle = true. Then in the firstViewController, in viewDidAppear, call the drawCircle method and then reset the shouldDrawCircle boolean. – Faisal Memon Sep 12 '18 at 08:40
  • My app its not totally completed yet but I just tried the method you said and I think this is just what I need. thank you very much for your help – K Y Sep 12 '18 at 08:59