[英]My Custom UIView is not removed from SuperView
I have one PopUpView which I add to a ViewController . 我有一个PopUpView ,我添加到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. 我创建了一个委托方法didTapOnOKPopUp(),这样当我点击PopUpView中的Ok Button时,它应该从使用它的委托的ViewController中删除。
Here is the code for PopUpView.Swift 这是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. 这是我使用委托方法的ForgotPasswordViewController的代码。
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())
}
// 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!
谢谢!
Each call to popupinstance()
creates a new PopUp
View. 每次调用
popupinstance()
都会创建一个新的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 : 但我认为在你的情况下使用
lazy var
更好:
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 现在每次调用
popUpInstance
都会返回相同的popUp实例
Every time you call .popUpInstance()
, it creates a brand new PopupView
instance, thus causing you to lose the reference to the previously created and added one in the view hierarchy. 每次调用
.popUpInstance()
,它都会创建一个全新的PopupView
实例,从而导致您丢失对视图层次结构中先前创建和添加的实例的引用。
Define popUpView
as an instance variable and you should be good to go: 将
popUpView
定义为实例变量,你应该好好去:
class ForgotPasswordViewController: UIViewController, PopUpViewDelegate {
private lazy var popupView: PopUpView = {
let popUpView = UINib(nibName: "PopUpView", bundle: nil)
.instantiate(withOwner: nil, options: nil)
.first as! PopUpView
popUpView.delegate = self
return popUpView
}()
@IBAction func btnSendTap(_ sender: UIButton) {
self.view.addSubview(self.popupView)
}
func didTapOnOKPopUp() {
self.popupView.removeFromSuperview()
}
}
Every time you call the function popUpInstance you create another instance of PopUpView, when you do so your delegate is not relevant. 每次调用popUpInstance函数时,都会创建另一个PopUpView实例,当你这样做时,你的委托就不相关了。
you can do this part of the code in some ways: 你可以通过某些方式完成这部分代码:
create the popUpInstance() function and save the instance as a class parameter 创建popUpInstance()函数并将实例保存为类参数
make a class parameter like this 像这样制作一个类参数
private lazy var popupView: PopUpView = { let popUpView = UINib(nibName: "PopUpView", bundle: nil).instantiate(withOwner: nil, options: nil).first as! PopUpView popUpView.delegate = self return popUpView }()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.