问题
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