abacaba
abacaba

Reputation: 133

Swift UIButton show and fade subview : Crashes on 'OKButtonPressed]: unrecognized selector sent'

I have made a UIButton on the storyboard, connected it to the ViewController. I want to programmatically make it show a 'hintView' on tapping, and then fade the 'hintView' by tapping an 'OKButton' on the 'hintView'. But it crashes when the 'OKButton' is pressed: Thread 1: "-[buttonToShowImgView.ViewController OKButtonPressed]: unrecognized selector sent to instance 0x7f93d6806940". What is wrong here?

    var hintView: UIImageView?

    @IBAction func buttonTapped(_ sender: Any) {
        showHint()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
 
    func showHint() {
        self.hintView = UIImageView(image: UIImage(named: "hintContent"))
        hintView!.frame = view.frame
        hintView!.isUserInteractionEnabled = true
        hintView!.alpha = 1.0
        view.addSubview(hintView!)
        addOKButton()
    }

    func addOKButton() {
        let OKButton = UIButton(type: .system)
        OKButton.setTitle("OK!", for: UIControl.State.normal)
        OKButton.setTitleColor(UIColor.red, for: .normal)
        OKButton.titleLabel!.font = UIFont(name: "Avenir", size: 88)
        OKButton.backgroundColor = UIColor.clear
        OKButton.frame = CGRect(x: 0 , y: hintView!.bounds.height*3/4, width: hintView!.bounds.width, height: hintView!.bounds.height/6)
        OKButton.addTarget(self, action: Selector(("OKButtonPressed")), for: UIControl.Event.touchUpInside)
        hintView!.addSubview(OKButton)
    }
    
    func OKButtonPressed() {
            self.hintView!.alpha = 0.0
    }

}

Upvotes: 0

Views: 36

Answers (3)

Devaki Nandan Sharma
Devaki Nandan Sharma

Reputation: 71

func addOKButton() {
    let OKButton = UIButton(type: .system)
    OKButton.setTitle("OK!", for: UIControl.State.normal)
    OKButton.setTitleColor(UIColor.red, for: .normal)
    OKButton.titleLabel!.font = UIFont(name: "Avenir", size: 88)
    OKButton.backgroundColor = UIColor.clear
    OKButton.frame = CGRect(x: 0 , y: hintView!.bounds.height*3/4, width: hintView!.bounds.width, height: hintView!.bounds.height/6)
    OKButton.addTarget(self, action: #selector(OKButtonPressed(_:)), for: .touchUpInside)
    hintView!.addSubview(OKButton)
}
  
@objc func OKButtonPressed(_ sender:UIButton) {
            self.hintView!.alpha = 0.0
    }

Upvotes: 0

Duncan C
Duncan C

Reputation: 131398

Use #selector(), not Selector(), to pass selectors to functions. The #selector() form lets the compiler check that the method is defined correctly.

In this particular case I think the problem is that your function lacks the @objc tag, which is needed to make a function have the dynamic dispatch required for it to work as a selector.

Note that you should also name functions beginning with lower-case names. Types and class names should start with upper-case letters, and function and variable names should start with lower-case. This is a strong convention in Swift.

Upvotes: 2

Dominic
Dominic

Reputation: 258

func addOKButton() {
    let OKButton = UIButton(type: .system)
    OKButton.setTitle("OK!", for: UIControl.State.normal)
    OKButton.setTitleColor(UIColor.red, for: .normal)
    OKButton.titleLabel!.font = UIFont(name: "Avenir", size: 88)
    OKButton.backgroundColor = UIColor.clear
    OKButton.frame = CGRect(x: 0 , y: hintView!.bounds.height*3/4, width: hintView!.bounds.width, height: hintView!.bounds.height/6)
    OKButton.addTarget(self, action: #selector(pressedOK()), for: .touchUpInside)
    hintView!.addSubview(OKButton)
}

@objc func pressedOK() {
    self.hintView!.alpha = 0.0
}

Upvotes: 1

Related Questions