0

I'm trying to do the following in my app: I made a file named "blur.swift", this file has code to blur the up side of a view so that everything that passes there gets "blurred". I have made this file the class to one view in Interface Builder. It worked perfectly, except for one detail: I cannot put a button via IB in the sae region that receives the blur, because the button also gets blurred and does not work, and I need to insert this button there because that is the button the user will press to return to the previous screen… so I decided to insert the button programmatically in the same file that has the code with the blur effect, now it worked! The blur is there and the button too!

The problem that I cannot solve is how to make this button actually works, I tried using "performSegueWithIdentifier" but I'm certain I'm missing something here, because it simple does not work! So I decided to ask for help here, below is the entire code from my "blur.swift" file.

Just to make things more clear: the segue with identifier "back" goes from the view that has the "blur.swift" class to the view controller (named giraanam) that I want to open when the user press the button.


@IBDesignable

class blur: UIView {

    override func layoutSubviews() {

        super.layoutSubviews()


        // Insert blur effect
        var blur:UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
        var effectView:UIVisualEffectView = UIVisualEffectView (effect: blur)
        effectView.frame = CGRectMake(0,0,320,69)
        self.addSubview(effectView)

        ///Insert the button
        let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
        button.frame = CGRectMake(20, 25, 34, 34)
        button.setBackgroundImage(UIImage(named:"Back_Resultados.png"),forState: UIControlState.Normal)
        button.performSegueWithIdentifier("back", sender: self)
        self.addSubview(button)


    }

}

Thanks to you all!

John Riselvato
  • 12,854
  • 5
  • 62
  • 89
Vladimir
  • 77
  • 2
  • 11

1 Answers1

0

You know that layoutSubviews gets called very frequently right? The code you're using would attempt to call performSegueWithIdentifier every time layoutSubviews is called. You want to instead create your button once and add a target to the button--something like this:

class blur: UIView {

    var button: UIButton?

    override func layoutSubviews() {

        super.layoutSubviews()

        // Insert blur effect
        var blur:UIBlurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
        var effectView:UIVisualEffectView = UIVisualEffectView (effect: blur)
        effectView.frame = CGRectMake(0,0,320,69)
        self.addSubview(effectView)

        // Insert the button if it doesn't exist already
        if !button {
            button = UIButton.buttonWithType(UIButtonType.System) as UIButton
            button.frame = CGRectMake(20, 25, 34, 34)
            button.setBackgroundImage(UIImage(named:"Back_Resultados.png"),forState: UIControlState.Normal)
            // Add self as a target
            button.addTarget(self, action: "didTapButton:", forControlEvents: .TouchUpInside)
            self.addSubview(button)
        }

    }

    func didTapButton(sender: UIButton!) {
        // Perform segue here or call out to a delegate to perform the segue
    }

}

Here's a SO post on how to programmatically make a button in Swift: Make a UIButton programmatically in Swift

You really should consider moving all of this code into a setup method that only gets called once when the view is created rather than on every call to layoutSubviews. Also, you probably want to add a delegate to this view class and set it to your view controller. Then implement a method in the view controller that can perform the segue for you.

(p.s. Some of my syntax may be off. I wrote this in the browser and I'm still wrestling with Swift)

Community
  • 1
  • 1
Matt Long
  • 24,438
  • 4
  • 73
  • 99