我有一个底部视图和一个顶视图。顶视图具有透明度。当顶视图被解除时,我想触发#selector
。我无法使用viewWillAppear
或viewDidAppear
,因为底部视图仍然可见。如何在底部视图中触发#selector
?什么会取代viewWillAppear
?
主视图后面有一个模糊视图。我使用self.modalPresentationStyle = .overCurrentContext
来展示视图。
答案 0 :(得分:1)
协议是最适合您的解决方案。 首先,您需要以这种方式创建协议
// MARK: - TopViewController Protocol
protocol TopViewControllerDelegate: NSObjectProtocol {
func topViewWillDismissWith(data: Dictionary<String,String>)
}
在TopViewController
的{{1}} make属性中,当您TopViewControllerDelegate
解雇时,您需要调用TopViewController
中的委托方法
didTapOnDismissTopView
现在是时候在class TopViewController : UIViewController {
weak var delegate : TopViewControllerDelegate!
// so on...
// MARK: - Action method
func didTapOnDismissTopView() {
var dict = [String : String]()
dict["Amount"] = "$20.00"
dict["ExpireIn"] = "20day"
dict["Location"] = "USA"
dict["message"] = "hello friend this is your Answer"
self.delegate.topViewWillDismissWith(data: dict) //Must write this line
self.dismiss(animated: true, completion: nil)
}
}
中实现TopViewControllerDelegate
协议,它看起来像是
BottomViewController
当您提交class BottomViewController : UIViewController,TopViewControllerDelegate {
// MARK: - Action method
func didTapOnDisplayTopView() {
let topVC = TopViewController()
topVC.delegate = self
self.present(topVC, animated: true, completion: nil)
}
// MARK: - TopViewControllerDelegate
// This method will call while topViewControllr Will Dismiss.
// if you want to pass data then pass otherwise you can remove perameter from method.
func topViewWillDismissWith(data: Dictionary<String, String>) {
// Do here which you want
}
}
时,您需要指定TopViewController
属性。当您的delegate
将在此时解散时topViewControllr
此方法将会调用。
希望它会对你有所帮助。