【发布时间】:2019-04-08 13:30:54
【问题描述】:
我有一个 PopUpView,我将其添加到 ViewController。
我创建了一个委托方法 didTapOnOKPopUp(),这样当我在 PopUpView 中单击 Ok 按钮时,它应该从使用它的委托的 ViewController 中删除。
这是 PopUpView.Swift
的代码protocol PopUpViewDelegate: class {
func didTapOnOKPopUp()
}
class PopUpView: UIView {
weak var delegate : PopUpViewDelegate?
@IBAction func btnOkPopUpTap(_ sender: UIButton)
{
delegate?.didTapOnOKPopUp()
}
}
这是我使用委托方法的 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()
}
}
我尝试了this,但没有成功!请帮我。谢谢!
【问题讨论】: