简体   繁体   中英

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?

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
        • 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM