Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My Custom UIView is not removed from SuperView

Tags:

ios

swift

uiview

I have one PopUpView which I add to a ViewController.

I have created a delegate Method didTapOnOKPopUp() so that when I click on Ok Button in the PopUpView, It should remove from the ViewController which using it's delegate.

Here is the code for PopUpView.Swift

protocol PopUpViewDelegate: class {
    func didTapOnOKPopUp()
}


class PopUpView: UIView {
weak var delegate : PopUpViewDelegate?

@IBAction func btnOkPopUpTap(_ sender: UIButton)
    {
        delegate?.didTapOnOKPopUp()
    }
}

Here is the code for ForgotPasswordViewController where I am using delegate method.

class ForgotPasswordViewController: UIViewController, PopUpViewDelegate {

// I have created an Instance for the PopUpView and assign Delegate also.

func popUpInstance() -> UIView {
        let popUpView = UINib(nibName: "PopUpView", bundle: nil).instantiate(withOwner: nil, options: nil).first as! PopUpView
        popUpView.delegate = self
        return popUpView
    }
// Here I am adding my view as Subview. It's added successfully.
@IBAction func btnSendTap(_ sender: UIButton) {
        self.view.addSubview(self.popUpInstance())
    }

It's added successfully

// But when I tapping on OK Button. My PopUpView is not removing from it's View Controller. 

func didTapOnOKPopUp() {

        self.popUpInstance().removeFromSuperview()
    }
}

I have tried this but No Success! Please Help me. Thanks!

like image 540
iHarshil Avatar asked Jan 02 '23 15:01

iHarshil


1 Answers

Each call to popupinstance() creates a new PopUp View.

You can either create a reference to the created pop up :

private var displayedPopUp: UIView?
@IBAction func btnSendTap(_ sender: UIButton) {
    displayedPopUp = self.popUpInstance()
    self.view.addSubview(displayedPopUp)
}


func didTapOnOKPopUp() {
    self.displayedPopUp?.removeFromSuperview()
    displayedPopUp = nil
}

But I think in your case using a lazy var is better :

replace

func popUpInstance() -> UIView {
        let popUpView = UINib(nibName: "PopUpView", bundle: nil).instantiate(withOwner: nil, options: nil).first as! PopUpView
        popUpView.delegate = self
        return popUpView
    }

by :

lazy var popUpInstance : UIView =  {
        let popUpView = UINib(nibName: "PopUpView", bundle: nil).instantiate(withOwner: nil, options: nil).first as! PopUpView
        popUpView.delegate = self
        return popUpView
    }()

Now every call to popUpInstance will return the same instance of your popUp

like image 183
CZ54 Avatar answered Jan 05 '23 18:01

CZ54