How to hide child view from parent view after some delay

407 views Asked by At

hide child view after few seconds I set time for that but i cant access child viewcontroller in my timer function I tried dissmiss , removefromparent about not worked. only self.view.isHidden = true is worked I can't place it in timer

My Parent view

enter image description here

Child View:

enter image description here

Button code: enter image description here

Timer code:enter image description here

1

There are 1 answers

0
DonMag On BEST ANSWER

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.