1

I Have the following constants which I want to use across my app:

let greenColor = UIColor(red: 173.0/255.0, green: 179.0/255.0, blue: 55.0/255.0, alpha: 1.0)
let goldColor = UIColor(red: 190.0/255.0, green: 164.0/255.0, blue: 104.0/255.0, alpha: 1.0)
let purpleColor = UIColor(red: 77.0/255.0, green: 92.0/255.0, blue: 165.0/255.0, alpha: 1.0)
let whiteColor  = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1.0)

However when I segue from ViewController which is a modally presented to another View controller I get the error above

Unexpectedly found nil while unwrapping an Optional value

The code I use for the segue is:

weak var pvc = self.presentingViewController
self.dismiss(animated: true, completion: {
    pvc?.present(AppHome(), animated: true, completion: nil)
})

I am stumped can someone tell me what I am doing wrong here?

EDIT

As per @rmaddy's suggestion the code that is causing the crash is this:

import UIKit

class AppHome: UIViewController {
    @IBOutlet weak var optionOneButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        optionOneButton.setButtonStyle(button: optionOneButton, color: purpleColor, title: "Current Knowledgebase")
    }

Here is an image of the error and the helper output in xCode: enter image description here

Justin Erswell
  • 688
  • 7
  • 42
  • 87
  • What line of code exactly is causing the error? – rmaddy Nov 14 '17 at 17:40
  • A line inside the ViewController itself `optionOneButton.setButtonStyle(button: optionOneButton, color: purpleColor, title: "Current Knowledgebase")` the error helper shows when hovering the `purpleColor` element – Justin Erswell Nov 14 '17 at 17:43
  • When posting about an error, post the actual code causing the error. None of the code you posted has anything to do with your error. `optionOneButton` is probable nil. See https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu?rq=1 – rmaddy Nov 14 '17 at 17:45
  • You should add the relevant code to the question.. I guess optionOneButton is nil – Yitzchak Nov 14 '17 at 17:46
  • What is the scope of the colors? Where they are defined? – Yitzchak Nov 14 '17 at 17:48
  • They are general constants held in a separate swift file – Justin Erswell Nov 14 '17 at 17:50
  • 3
    The default initializer `AppHome()` creates a brand new instance which is not the instance in the storyboard and which won't connect the outlet. And it's highly recommended to encapsulate the colors in a struct. – vadian Nov 14 '17 at 17:55
  • Put a breakpoint on the line with the error and examine the variables. I'm reasonably sure you'll find that @vadian is correct, but it's a useful exercise anyway. – Phillip Mills Nov 14 '17 at 19:22
  • As an aside, as of Xcode 9 you can define colors in your asset catalog and reference them using `UIColor(named:)`: https://stackoverflow.com/questions/44397680/how-can-we-use-assets-catalog-colors-sets and https://developer.apple.com/documentation/uikit/uicolor/2877380-init – mharper Nov 14 '17 at 21:39

0 Answers0