“线程1:致命错误:展开一个可选值时意外发现nil”-SWIFT

时间:2018-08-23 16:12:44

标签: ios swift

我正在尝试使用UIViewController显示自定义弹出菜单。我编写了一个函数,以允许重复使用显示弹出窗口。但是,我一直收到上面的错误,并且我不知道如何处理“ 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) 

}

我的msg参数不是optioanl类型。我有些困惑。

1 个答案:

答案 0 :(得分:1)

messageLabelnil,直到加载视图为止(即使已正确连接)

class PopUpViewController:UIViewController {
  var sendedText = ""
}

然后使用

popUpVC.sendedText = msg  

//

然后将sendedText设置为viewDidLoad内的lbl

相关问题