我在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
@IBAction func closePopup(sender: AnyObject) {
self.removeAnimate()
// Need to Close down the blur effect here!
}
}当我关闭弹出窗口时,主视图控制器仍然是模糊的。如何从closePopup IBAction中移除此模糊视图
发布于 2015-03-10 22:28:24
使用完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)
}发布于 2015-03-10 22:31:39
您的问题似乎是如何在两个ViewControllers之间传递信息。尽管有许多方法可以做到这一点,但我将向您展示如何使用带有基金会的NotificationCenter的观察者模式来完成此任务。在PopUpViewController中,在closePopUp中添加通知
@IBAction func closePopup(sender: AnyObject) {
self.removeAnimate()
NSNotificationCenter.defaultCenter().postNotification(
NSNotification(name: "popUpDidClose", object: self)
)
}回到你的MainViewController,在触发你的PopUpViewController之前,在你的viewDidLoad方法中添加一个观察者。
override func viewDidLoad() {
//...
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("clearBlurEffect:"),
name: "popUpDidClose",
object: nil
)
}最后,将方法clearBlurEffect添加到MainViewController中,这将与NSNotificationCenter的addObserver方法中的selector参数相对应。
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。
https://stackoverflow.com/questions/28965838
复制相似问题