swift - 从父 View Controller 向 subview Controller 发送数据

标签 swift uitableview popup viewcontroller childviewcontroller

我正在尝试将数据从父 View Controller 发送到 subview Controller ,每当我覆盖 performSegue 方法时,数据都会加载到弹出 View ,如下所示:

enter image description here

但我想要的是显示如下图所示的数据: enter image description here

我已经通过 didSelectRowAt indexPath 方法将弹出 View 添加到主视图,我使用了协议(protocol)方法,但它没有加载数据。

我的父 View Controller 代码如下所示:

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)!]
}

选择segue线和

enter image description here

在popupVC中选择主视图,并使其背景色透明

enter image description here

关于swift - 从父 View Controller 向 subview Controller 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49004679/

相关文章:

ios - SwiftUI - 如何从单独的类触发警报

swift - Swift 中的核心数据同步

ios - 可以使 UIGestureRecognizer 在 AVPlayerViewController 中工作吗?

iphone - cellForRowAtIndexPath 返回 nil

swift - 如何将 UITableView 作为 subview 添加到 xib 中的 UIView?

ios - 展开时注册表格单元格为零

javascript - jquery 弹出窗口仅在 ie 中有效,在 firefox 中无效

ios - 来电消失查看事件

jquery - 带有html5的简单可拖动弹出窗口

iframe 中的 Javascript window.opener