0

添加了一个子视图控制器,如下所示:

// ParentViewController
 if ((self.delegate?.showPopupSymtomDetailViewController(view: viewController, receiver: self)) != nil)
    {

    }

// ChildViewController
 //MARK: PopUpCode
func popUpAt(parentViewController:UIViewController)
{
    parentViewController.addChildViewController(self)
    parentViewController.view.addSubview(self.view)

    self.view.frame = parentViewController.view.frame
    self.didMove(toParentViewController: parentViewController)

}

如何将数据发送回 parentViewController,请指导。

4

1 回答 1

0

例如,您可以使用回调。

class ParentViewController: UIViewController {

  if ((self.delegate?.showPopupSymtomDetailViewController(view: viewController, receiver: self)) != nil) {
     viewController = { YourString in 
          //do here whatever you want in your parent view controller
        }
  }
}

class ChildViewController: UIViewController {
   var callbackFunc: ((String) -> Void)? //You can decide if the object you want to send back, I used String but you can change it and even you can send more than one parameters back (String, ThirViewController, Bool)...

   func popUpAt(parentViewController:UIViewController) {
          parentViewController.addChildViewController(self)
          parentViewController.view.addSubview(self.view)

          self.view.frame = parentViewController.view.frame
          self.didMove(toParentViewController: parentViewController)
          callBack(YourString) //Here you pass the data from ChildViewController to ParentViewController
   }
}

还有其他方法可以将信息从 ChildViewController 发送回 ParentViewController,但这取决于您的项目。这是你要经常做的动作吗?也许你需要一个委托来重用它。

于 2017-10-27T13:14:49.750 回答