从父视图控制器向子视图控制器发送数据
Sending Data to child view controller from parent view controller
我正在尝试将数据从父视图控制器发送到子视图控制器,每当我覆盖 performSegue 方法时,数据都会加载到弹出视图,如下所示:
但我想要的是显示数据,如下图所示:
我已经通过didSelectRowAt indexPath方法将弹出视图添加到主视图,我使用了协议方法,但它没有加载数据。
我的父视图控制器代码如下所示:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! TableViewCell
cell.customInit(noticeTitle: notificatonData[indexPath.row].notice_title,noticeDiscripetion: notificatonData[indexPath.row].notice_desc)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopUpViewController")
self.addChildViewController(popOverVC)
popOverVC.view.frame = view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: self)
performSegue(withIdentifier: "goToPopUpView", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? PopUpViewController{
destination.notificationfianlData = notificatonData[(tableView.indexPathForSelectedRow?.row)!]
}
}
你应该使用 segue 或 child
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToPopUpView", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? PopUpViewController{
destination.notificationfianlData = notificatonData[(tableView.indexPathForSelectedRow?.row)!]
}
select segue 行和
select popupVC 中的主视图并使其背景色透明
请试试这个。应该不需要 performSegue。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopUpViewController")
self.view.addSubview(popOverVC.view)
popOverVC.notificationfianlData = notificatonData[indexPath.row]
popOverVC.view.frame = view.bounds
}
仅供参考。使 PopUpViewController 视图,背景颜色透明。会这样显示。
我正在尝试将数据从父视图控制器发送到子视图控制器,每当我覆盖 performSegue 方法时,数据都会加载到弹出视图,如下所示:
但我想要的是显示数据,如下图所示:
我已经通过didSelectRowAt indexPath方法将弹出视图添加到主视图,我使用了协议方法,但它没有加载数据。
我的父视图控制器代码如下所示:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! TableViewCell
cell.customInit(noticeTitle: notificatonData[indexPath.row].notice_title,noticeDiscripetion: notificatonData[indexPath.row].notice_desc)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopUpViewController")
self.addChildViewController(popOverVC)
popOverVC.view.frame = view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: self)
performSegue(withIdentifier: "goToPopUpView", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? PopUpViewController{
destination.notificationfianlData = notificatonData[(tableView.indexPathForSelectedRow?.row)!]
}
}
你应该使用 segue 或 child
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToPopUpView", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? PopUpViewController{
destination.notificationfianlData = notificatonData[(tableView.indexPathForSelectedRow?.row)!]
}
select segue 行和
select popupVC 中的主视图并使其背景色透明
请试试这个。应该不需要 performSegue。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopUpViewController")
self.view.addSubview(popOverVC.view)
popOverVC.notificationfianlData = notificatonData[indexPath.row]
popOverVC.view.frame = view.bounds
}
仅供参考。使 PopUpViewController 视图,背景颜色透明。会这样显示。