Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Simple iPhone App with Swift Views and View Controllers View Controllers and IBAction

Arjun Amin
Arjun Amin
2,228 Points

In an IBAction what is 'Sender' and 'AnyObject'?

what do the sender refer to in our view or anywhere in the code?

1 Answer

Sebastien Thomas
Sebastien Thomas
16,502 Points

Hi Arjun,

The sender is the object that called the action. So let's say if you have 2 UIButtons calling the same function buttonPressed. You can then use the sender parameter to find out, using tags, which button was pressed for example :

@IBAction func buttonPressed(sender: AnyObject) {

    let button = sender as UIButton
    println("Button \(button.tag) was pressed!")
}


Hope this helps
Arjun Amin
Arjun Amin
2,228 Points

thanks sebastian