I have an IBOutlet in ViewController.swift called backgroundView
class ViewController: UIViewController, SideBarDelegate {
@IBOutlet weak var backgroundView: UIView!
And I want to use that IBOutlet on SideBar.swift
@objc protocol SideBarDelegate{
func sideBarDidSelectButtonAtIndex(index:Int)
optional func sideBarWillClose()
optional func sideBarWillOpen()
}
//When an item of the sidebar is selected, and also when the sidebar will open or close
class SideBar: NSObject, SideBarTableViewControllerDelegate {
func handleSwipe(recognizer:UISwipeGestureRecognizer){
let bgv = ViewController()
if recognizer.direction == UISwipeGestureRecognizerDirection.Right {
showSideBar(false)
delegate?.sideBarWillClose?()
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = bgv.backgroundView.bounds
bgv.backgroundView.addSubview(blurView)
} else {
showSideBar(true)
delegate?.sideBarWillOpen?()
}
}
But when showing the side bar, the background doesn't blur. What is wrong?
You're not actually accessing that view controller's instance. You create a new one and assign it to
bgv
, and then you modify that one.You could access it through the delegate but not by creating a new view controller. You'd also have to add it as a variable to your protocol.
A better idea would be to move the stuff that the View Controller should handle to that class instead of trying to access views of that controller. That totally defeats the purpose of delegation.
All that code should go in
sideBarWillClose
in your delegate (the view controller's implementation of that method)I'd also recommend not making those functions optional, as you're going to want the parent controller to be able to do things when the menu opens and closes. Plus that cleans up your code a bit, fewer ?'s