In Like_btn_Action()
function, you:
- create an instance of
LikeViewController
- add it as a child view controller
- add its view to your view
- set that view's background color
and then the function exits. At this point, you no longer have a reference to your instance of LikeViewController
... likeVC
has gone out of scope.
You need to use a class-level var to maintain the reference to the loaded child view controller, along these lines:
var likeVC: LikeViewController?
@IBAction func Like_btn_Action(_ sender: Any) {
likeVC = self.storyboard?.instantiateViewController( etc ...)
}
Then, when you want to remove the view you added, you can "get to it" via:
likeVC.view.removeFromSuperview()
for example.