将数据从表视图传递到视图控制器

时间:2017-06-02 07:34:38

标签: ios uitableview swift3

当我点击该行时,我会执行一项功能,然后将转到另一个视图控制器以显示详细信息,但是会出现错误

这是错误=> 致命错误:在解包可选值时意外发现nil

这是我在桌面视图上的代码didselectRow

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popUp") as! PopupViewController
    let pro_name = preCakeArray[indexPath.row]["pro_name"]
    popup.popUpTitle.text = pro_name as? String
    self.addChildViewController(popup)
    popup.view.frame =  self.view.bounds
    self.view.addSubview(popup.view)
    popup.didMove(toParentViewController: self)
    }

这是我的ViewController来显示像弹出窗口一样的细节

class PopupViewController: UIViewController {

@IBOutlet weak var popUpImg: UIImageView!
@IBOutlet weak var popUpTitle: UILabel!
@IBOutlet weak var popUpDes: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
        }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func closeBtn(_ sender: Any) {
    self.view.removeFromSuperview()
}

1 个答案:

答案 0 :(得分:2)

解决方案1 ​​

您无法在addChildViewController中设置标签文本,或者您需要将字符串传递给viewcontroller并将该字符串设置为标签。

class PopupViewController: UIViewController {

 var strPopTitle: String?

override func viewDidLoad() {
    super.viewDidLoad()
        self.popUpTitle.text = strPopTitle
        }