从子视图返回时,Viewdidappear不会调用

时间:2017-07-18 16:09:45

标签: ios iphone swift

我知道这是一个虚拟问题,但我不知道问题是什么。我有两个viewcontrollers,其中一个是作为弹出窗口打开。当我在弹出窗口中单击关闭按钮时,我调用unwind segue但是viewdidappear函数没有在我的父视图控制器中调用。  我从这篇文章得到了帮助。 Unwound by a child

 override func viewDidAppear(_ animated: Bool)
{
    // Handle controller being exposed from push/present or pop/dismiss
    if (self.isMovingToParentViewController || self.isBeingPresented){
        // Controller is being pushed on or presented.
        print("hello")
    }
    else{
        // Controller is being shown as result of pop/dismiss/unwind.
        print("hello2")
    }
}

1 个答案:

答案 0 :(得分:0)

我通过委托和协议解决了这个问题。

在tableviewcontroller的secondviewcontroller中:

protocol TableViewControllerDelegate { 
    func controller(_ controller: TableViewController, didAddItem: String)
}
class TableViewController:  UIViewController, UITableViewDelegate,UITableViewDataSource {

    var delegate: TableViewControllerDelegate?
    ...

在secondviewcontroller的segue部分

delegate?.controller(self, didAddItem: "hello")

在firstviewcontroller中:

class MainViewController: UIViewController, TableViewControllerDelegate{

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "Segue" {
            let destination = segue.destination as? TableViewController
            if let viewController = destination
            {
                viewController.delegate = self as TableViewControllerDelegate
            }
        }
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    // MARK: Add Item View Controller Delegate Methods

    func controller(_ controller: TableViewController, didAddItem: String) {
        // Update Data Source

        // Reload Table View
        mainStackView.isHidden = false
        // Dismiss Add Item View Controller
        dismiss(animated: true)
    }
}