3

I have two view controllers. The firstVC is being loaded when the app launches, then if a certain button was tapped then secondVC is being called using this code:

    let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "tagsStory") as! TagsVC
    self.addChildViewController(popOverVC)
    popOverVC.view.frame = self.view.frame
    self.view.addSubview(popOverVC.view)
    popOverVC.didMove(toParentViewController: self)

In the secondV when a certain button is tapped the secondVC gets removed and firstVC gets back to the user via this code:

self.view.removeFromSuperview()

My question is, how do I get notified when the secondVC has been removed inside the firstVC?

I tried to use, viewDidAppear, viewWillAppear, willMove... but nothing worked yet.

Marton Zeisler
  • 199
  • 3
  • 15

3 Answers3

2

If you simply want to make a function call, simply use notification. In your parent view controller, register notification to receive function calls.

NotificationCenter.default.addObserver(self, selector: #selector(listnerFunction), name: NSNotification.Name(rawValue: "notificationName"), object: nil)

func listnerFunction() {
    tableView.reloadData()
}

Done forget to remove this listener when the view controller is destroyed

deinit {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "notificationName"), object: nil)
}

Then in your child view controller, when you are about to remove it from parent, call the registered notification funtion in this way

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: nil) 
Fangming
  • 24,551
  • 6
  • 100
  • 90
1

In the secondVC when a certain button is tapped the secondVC gets removed and firstVC gets back to the user that is already notified you, You can make the delegate for this or fire the notification if the case is one to many.

Salman Ghumsani
  • 3,647
  • 2
  • 21
  • 34
  • I just want to reload the tableview but I can only do that after firstVC is loaded so that's why I am asking, so I would need some sort of function for firstVC which gets called immediately after the secondVC was removed so I can call tableview.reloadData() – Marton Zeisler Jul 13 '17 at 12:05
  • Yes, you can call the tableview.reloadData() before calling removeFromSuperview(), only you have to get the instance of firstVC in the secondVC and you can get the instance firstVC by many ways.. firstVCInstance.tableview.reloadData() – Salman Ghumsani Jul 13 '17 at 12:13
1

This is covered in the UIViewController documentation.

Your firstVc is a container view controller, so it has certain responsibilities:

Your container view controller must associate a child view controller with itself before adding the child's root view to the view hierarchy. This allows iOS to properly route events to child view controllers and the views those controllers manage. Likewise, after it removes a child's root view from its view hierarchy, it should disconnect that child view controller from itself. To make or break these associations, your container calls specific methods defined by the base class. These methods are not intended to be called by clients of your container class; they are to be used only by your container's implementation to provide the expected containment behavior.

You are doing the right thing when you add the second view controller by calling didMove(toParentViewControler:) before you add the subview but you also need to call removeFromParentViewController() after you remove the subview.

If you do this then the viewDid/WillDisappear methods will be called.

Paulw11
  • 108,386
  • 14
  • 159
  • 186