Swift将选择器从另一个视图

时间:2016-12-14 22:52:48

标签: ios swift xcode

我有一个底部视图和一个顶视图。顶视图具有透明度。当顶视图被解除时,我想触发#selector。我无法使用viewWillAppearviewDidAppear,因为底部视图仍然可见。如何在底部视图中触发#selector?什么会取代viewWillAppear

主视图后面有一个模糊视图。我使用self.modalPresentationStyle = .overCurrentContext来展示视图。

image

1 个答案:

答案 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此方法将会调用。 希望它会对你有所帮助。