Adam Young
Adam Young

Reputation: 1331

How to get source (sender) for an event in Swift

I have a function

@IBAction func fblogin(sender: AnyObject) {
println (sender)
}

The action of two button is this function. If I print sender nil gets printer. How do I check which button called it. Do I need to create outlet for buttons or how do I create ids for buttons?

Upvotes: 3

Views: 5017

Answers (1)

derdida
derdida

Reputation: 14904

You could use sender's tag value

Just connect both buttons to your action, and check with:

    if(sender.tag == 1) {
    ... button 1
    } else if( sender.tag == 2) {
    .. button 2
    }

And use sender from type UIButton (you can select it directly in Xcode too)

  • how set tag value?
    • code
      • someBtn.tag = 1;
        • normally inside ViewDidLoad
    • UI
      • XCode->Storyboard->select UI (your UIButton) -> right side panel(Inspector) -> Attribute Inspector -> View -> Tag
        • enter image description here

Upvotes: 2

Related Questions