using IBOutlet from another class in swift

2019-05-11 04:37发布

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?

2条回答
Melony?
2楼-- · 2019-05-11 05:23
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)
    }
}
查看更多
Deceive 欺骗
3楼-- · 2019-05-11 05:34

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.

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

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

查看更多
登录 后发表回答