Search code examples
iosswifttimerparentviewcontroller

How to hide child view from parent view after some delay


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


Solution

  • 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.