0

I have a little problem with my addTarget in UIButton. When I run my app, my function is automatically launched, while he is necessary to press the UIButton to launch. I don't know where is my problem.

My Button

let BTN_happy : UIButton = UIButton()
    BTN_happy.frame = CGRectMake(0, rowHeight * 1, fullWidth, rowHeight)
    BTN_happy.backgroundColor = UIColor(hexString: "#ff3b30")
    BTN_happy.addTarget(self, action: Selector(data("Happy")), forControlEvents: .TouchUpInside)

    self.view.addSubview(BTN_happy)
    text("Happy", button: BTN_happy)

My Function

func text(text: String, button: UIButton) {

        let txt : UILabel = UILabel()
        txt.text = text.uppercaseString
        txt.frame = CGRectMake(20, 0, fullWidth - 40, button.bounds.size.height)
        txt.textColor = UIColor(hexString: "#ffffff")
        txt.font = UIFont.systemFontOfSize(30, weight: UIFontWeightHeavy)
        txt.shadowOffset = CGSize(width: 2, height: 2)
        txt.shadowColor = UIColor(hexString: "#000000", alpha: 0.3)

        button.addSubview(txt)

    }

    func data(mood: String) {
        NSLog("Mood: \(mood)")
    }

Thank you in advance for your response.

Kevin Py
  • 2,459
  • 2
  • 22
  • 33

2 Answers2

1

You can't add a selector like that way. You need to change it like:

myBTN.addTarget(self, action: Selector("data:"), forControlEvents: .TouchUpInside)

Also change the method signature like:

func data(sender: UIButton)
{
   NSLog("Button clicked")
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
0

For resolve my problem, I use tag in my UIButton.

Kevin Py
  • 2,459
  • 2
  • 22
  • 33