Passing data from table view to view controller

余生长醉 提交于 2019-12-25 14:39:47

问题


i am do a function when i click on the row and then will go to another view controller to show the detail but it's get an error

this is the error => fatal error: unexpectedly found nil while unwrapping an Optional value

this is my code on table view 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)
    }

and this is my ViewController to show the detail like popup

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:


Solution 1

You can't set label text while addChildViewController or you need to pass string to viewcontroller and set that string to label .

class PopupViewController: UIViewController {

 var strPopTitle: String?

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


来源:https://stackoverflow.com/questions/44323291/passing-data-from-table-view-to-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!