vyudi
vyudi

Reputation: 838

Unwrapping UILabel always returns nil

I've hooked a UILabel to my VC using the storyboard, does generating a weak var but connected to a storyboard reference. Anyway, whenever I try to unwrap it, all I get is a nil value. What confuses me the most is that I'm able to access it on another method. I've already safe unwrapped it, it avoids the compiler errors, but doesn't solve my problem.

The compiler message is: fatal error: unexpectedly found nil while unwrapping an Optional value

Here is some of the code:

@IBOutlet weak var linesLeftCountLabel: UILabel!

override func viewDidLoad() {

      super.viewDidLoad()

      skView = SKView(frame: sceneView.frame)
      skView.multipleTouchEnabled = false

      //Create and configure the scene.
      scene = LogoRefactoryScene(size: skView.bounds.size)
      scene.scaleMode = .AspectFill
      skView.presentScene(scene)

      tapGesture = UITapGestureRecognizer(target: self, action: "eraseLine:")
      tapGesture.delegate = self
      view.addGestureRecognizer(tapGesture)

      view.addSubview(skView)
    }

Here I'm able to access it:

  @IBAction func colorPickerOfColor(sender: UIButton) {

    var pickerName = ""

    switch sender.tag {

    case 0:
      pickerName = "green"

    case 1:
      pickerName = "red"

    case 2:
      pickerName = "blue"

    default:
      pickerName = "orange"
    }

    pickerName += "ColorPicker"
    colorPickerView.image = UIImage(named: pickerName)

    currentColorName = ColorSelection.fromRaw(sender.tag + 1)!.colorName

    let color = ColorSelection.fromRaw(sender.tag + 1)!.getSKColor()
    scene.lineColor = color
    linesLeftCountLabel.textColor = color
  }

Here is where the compiler complains:

  func lineHasBeenDrawn() {

    linesLeftCountLabel.text = "Any String"
  }

Upvotes: 0

Views: 588

Answers (2)

dhin
dhin

Reputation: 473

Please check if you have connected the referencing outlet in the storyboard to the IBOutlet of your code

Upvotes: 0

vyudi
vyudi

Reputation: 838

Sorry for that guys. lineHasDrawn is a delegate method, that's why the strange behaviour. My bad.

Upvotes: 0

Related Questions