我有一个函数,我在viewDidLoad
MainViewcontroller
中调用
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
来关闭此视图控制器
@IBAction func closePopup(sender: AnyObject) {
self.removeAnimate()
// Need to Close down the blur effect here!
}
}
当我关闭弹出窗口时,主视图控制器仍然模糊。如何从closePopup
IBAction
?
答案 0 :(得分:1)
完成使用后,您必须删除blurEffectView
。在方法closePopUp
方法内部试试这个:
blurEffectView.removeFromSuperview()
当然,以下方式保存对此变量的引用,例如:
class PopUpViewController {
var blurEffectView : UIVisualEffectView!
@IBAction func closePopup(sender: AnyObject) {
self.removeAnimate()
blurEffectView.removeFromSuperview()
}
}
在此之前你必须这样做:
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)
// In this line you pass the reference to the Blur
self.popViewController.blurEffectView = blurEffectView
self.popViewController.showInView(self.view, withImage: UIImage(named: "typpzDemo"), withMessage: "You just triggered a great popup window", animated: true)
}
答案 1 :(得分:1)
您的问题似乎是如何在两个ViewController之间传递信息。虽然您可以采取多种方法,但我将使用观察者模式与基金会NotificationCenter
向您展示如何做到这一点。在PopUpViewController
中,在closePopUp
:
@IBAction func closePopup(sender: AnyObject) {
self.removeAnimate()
NSNotificationCenter.defaultCenter().postNotification(
NSNotification(name: "popUpDidClose", object: self)
)
}
返回MainViewController
,在启动viewDidLoad
之前,在PopUpViewController
方法中添加观察服务器。
override func viewDidLoad() {
//...
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("clearBlurEffect:"),
name: "popUpDidClose",
object: nil
)
}
最后,将方法clearBlurEffect
添加到MainViewController
,这将与selector
的{{1}}方法中的NSNotificationCenter
参数相对应。编辑:您必须从主队列中发出通知。
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()
}
}
}
}
。