ios - 弹出 Controller 关闭后删除 UIBlurView

标签 ios swift popup uiblureffect

我在 MainViewcontrollerviewDidLoad 中调用了一个函数

func showPopUp() {    

    self.popViewController = PopUpViewController(nibName: "PopUpViewController", bundle: nil)
    //self.popViewController.title = "This is a popup view"        

    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = view.bounds
    view.addSubview(blurEffectView)       

    self.popViewController.showInView(self.view, withImage: UIImage(named: "typpzDemo"), withMessage: "You just triggered a great popup window", animated: true)  
}

这会调出我的 PopupViewController 并模糊 MainViewController。在我的 PopupViewController 中,我有一个 IBAction 来关闭这个 View Controller

@IBAction func closePopup(sender: AnyObject) {
    self.removeAnimate()
   // Need to Close down the blur effect here!
    }
}

当我关闭弹出窗口时,主视图 Controller 仍然模糊不清。如何从 closePopup IBAction 中删除这个模糊 View ?

最佳答案

您的问题似乎是如何在两个 ViewController 之间传递信息。尽管您可以采用多种方法来实现这一点,但我将向您展示如何使用带有 Foundation 的 NotificationCenter 的观察者模式来做到这一点。在您的 PopUpViewController 中,在 closePopUp 中添加一个 Notification:

    @IBAction func closePopup(sender: AnyObject) {
        self.removeAnimate()
        NSNotificationCenter.defaultCenter().postNotification(
            NSNotification(name: "popUpDidClose", object: self)
        )
    }

回到您的 MainViewController,在您的 PopUpViewController 触发之前,将一个 Observer 添加到您的 viewDidLoad 方法中。

    override func viewDidLoad() {
        //...
        NSNotificationCenter.defaultCenter().addObserver(self,
                selector: Selector("clearBlurEffect:"),
                name: "popUpDidClose",
                object: nil
        )
    }

最后,将clearBlurEffect方法添加到MainViewController中,对应NSNotificationCenter中的selector参数' s addObserver 方法。编辑:您必须从主队列中触发通知。

    func clearBlurEffect(sender: NSNotification) {
        dispatch_async(dispatch_get_main_queue()) { [unowned self] in
            for subview in self.view.subviews as [UIView] {
                if let v = subview as? UIVisualEffectView {
                    v.removeFromSuperview()
                }
            }
        }
    }

在这里,我们从您的主视图中消除所有 UIVisualEffectView

关于ios - 弹出 Controller 关闭后删除 UIBlurView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28965838/

相关文章:

ios - CorePlot - 多行属性标签

ios - 如何启用单点触摸来处理 UILongPressGestureRecognizer 和 UIPanGestureRecognizer?

ios - swift 用户界面 : How to get a Navigation Title aligned with Back Button

ios - 使用tableview函数时出现错误

qt4 - 在 QT 4.6 w/Webkit 中:如何处理弹出窗口请求(WebView::createWindow)?

ios - 当我需要从两个不同的 Web 服务获取 Controller 数据时,我应该如何使用 MVVM 和依赖注入(inject)?

Android fragment 在 iOS 的 swift 中模拟

ios - Swift 3 - 向 UIView 弹出窗口添加文本

javascript - 在 Asp.net 页面上为什么 jquery 关闭按钮不起作用

ios - TextField 第一个单元格结果显示在 Tableview 单元格的其他单元格中