将数据从表视图传递到视图控制器 [英] Passing data from table view to view controller
本文介绍了将数据从表视图传递到视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我单击该行时,我正在执行一个功能,然后将转到另一个视图控制器以显示详细信息,但出现错误
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
这是错误 => 致命错误:在展开可选值时意外发现 nil
这是我在表视图 didselectRow 上的代码
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)
}
这是我的 ViewController,用于显示弹出窗口等细节
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
您不能在 addChildViewController 时设置标签文本,或者您需要将字符串传递给视图控制器并将该字符串设置为 label .
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
}
这篇关于将数据从表视图传递到视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文