0

I am trying to display a custom popup menu using a UIViewController. I have written a function to enable the re-use of displaying the popup. However, I keep getting the error above and I do not know how to handle the "nil".

SWIFT:

 func showPopUp(msg: String){

    let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
    let popUpVC = storyboard.instantiateViewController(withIdentifier: "popupEmpty") as! PopUpViewController
    popUpVC.messageLabel.text = msg  // ""Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" - SWIFT"
    self.present(popUpVC, animated: true, completion: nil) 

}

My msg parameter is not an optioanl type. I am somewhat confused.

johnDoe
  • 709
  • 11
  • 29
  • `messageLabel` isn't loaded yet. Use a `var messageLabelString` instead, and do `popUpVC.messageLabelString = msg` and in `viewDidLoad()` do `popUpVC.messageLabel.text = messageLabelString`. – Larme Aug 23 '18 at 16:15

1 Answers1

1

messageLabelis nil until view loads ( even if connected properly )

class PopUpViewController:UIViewController {
  var sendedText = ""
}

then use

popUpVC.sendedText = msg  

//

then set sendedText to the lbl inside viewDidLoad

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87