swiftuitableviewpopupviewcontrollerchildviewcontroller

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:

enter image description here

But what I want is show data in something like picture shown below: enter image description here

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

Solution

  • 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

    enter image description here

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

    enter image description here