Using IBOutlet from another class in fast

I have an IBOutlet in ViewController.swift called backgroundView

class ViewController: UIViewController, SideBarDelegate {

    @IBOutlet weak var backgroundView: UIView!

And I want to use this 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 the sidebar is displayed, the background does not blur. What's wrong?

+3
source share
2 answers

In fact, you are not accessing the view manager instance. You create a new one and assign it bgv, and then change it.

You can access it through a delegate, but not by creating a new view controller. You should also add it as a variable to your protocol.

, , , . .

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

sideBarWillClose ( ).

, , -, . , , ?

0
class ViewController: UIViewController, SideBarDelegate {

    @IBOutlet weak var backgroundView: UIView!

    var sideBar:SideBar = SideBar()

    override func viewDidLoad() { //show side bar or not

        sideBar = SideBar(sourceView: self.view, menuItems: ["first item", "second item", "funny item"])
        sideBar.delegate = self
    }

    func sideBarDidSelectButtonAtIndex(index: Int) { //which menuitem you take
        if index == 2 {
           // imageView.backgroundColor   = UIColor.redColor()
            //imageView.image             = nil
        } else if index == 0 {
            //imageView.image = UIImage(named: "stars")
        }
    }

    func sideBarWillOpen() {
        let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
        let blurView = UIVisualEffectView(effect: blurEffect)
        blurView.frame = backgroundView.bounds
        backgroundView.addSubview(blurView)
    }
}
+2

Source: https://habr.com/ru/post/1621234/


All Articles