我在 MainViewcontroller
的 viewDidLoad
中调用了一个函数
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/