简体   繁体   中英

Sending Data to child view controller from parent view controller

I am trying to send data from parent view controller to child view controller, whenever I override the performSegue method, the data loads to the popup view as shown below:

在此处输入图片说明

But what I want is show data in something like picture shown below: 在此处输入图片说明

I have added the popup view to the main view from didSelectRowAt indexPath method, I used the protocol method, but it didn't load the data.

my code for parent view controller is shown below:

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

You should either use segue or 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 line and

在此处输入图片说明

select the main view in the popupVC and make it's background color transparent

在此处输入图片说明

Please try with this one. There should be no need of 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
}

FYI. Make PopUpViewController View, background color transparent. It will show like this.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM