我有一個底視圖和一個頂視圖。頂視圖具有透明度。當頂視圖被解僱時,我想觸發一個#selector
。我無法使用viewWillAppear
或viewDidAppear
,因爲底部視圖仍然可見。如何在底部視圖中觸發#selector
?有些東西會取代viewWillAppear
?Swift將選擇器觸發到另一個視圖的視圖
主視圖背後有模糊的視圖。我使用self.modalPresentationStyle = .overCurrentContext
來呈現視圖。
我有一個底視圖和一個頂視圖。頂視圖具有透明度。當頂視圖被解僱時,我想觸發一個#selector
。我無法使用viewWillAppear
或viewDidAppear
,因爲底部視圖仍然可見。如何在底部視圖中觸發#selector
?有些東西會取代viewWillAppear
?Swift將選擇器觸發到另一個視圖的視圖
主視圖背後有模糊的視圖。我使用self.modalPresentationStyle = .overCurrentContext
來呈現視圖。
協議是您最佳的解決方案。 首先,你需要這樣的方式來創建協議
// MARK: - TopViewController Protocol
protocol TopViewControllerDelegate: NSObjectProtocol {
func topViewWillDismissWith(data: Dictionary<String,String>)
}
在你TopViewController
使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
將在當時解僱topViewWillDismissWith
此方法將調用。 希望它能幫助你。
如果您有描繪佈局的圖像,這可能會有幫助。 – toddg
@toddg圖像被添加 – pprevalon
所以頂視圖沒有參考底視圖?你最好的選擇可能是協議委託方法 – toddg