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

@IBAction & @IBOutlet

Hi, I have a @IBAction from a UIButton with a sender: I also have @IBOutlet fronm the same button. Why does this cause an error?

can you put up the code for us to help you ..

I will as soon as I get home this evening.

Thanks

12 Answers

whatever you name it shouldn't matter, as long as you are properly referring to them throughout your code...it's really more for your own organization and not confusing them.

as far as your code is concerned...I'm novice myself...so I couldn't help much there. haha! I'm glad you figured it out though!

without seeing your code, are you naming the action and the outlet different names? If it's the same, then that would cause an error

Taylor, They do have different names for example the name for the IBAction is orangeBtnPushed and the IBOutlet is orangedBtnPressed. I'm new to iOS bevelopment and the Interaction builder.

Thanks

I think that is it. I'll check when I get home to be sure. I will say this that I am pretty sure that the connection and name correctly because I have deleted them and redone them to make sure. But I do have the IBAction and IBOutlet named differently on the same button.

interesting..ok..well let us know when you get home!

var buttonSender = UIButton()

 @IBOutlet weak var orangeBtnPicked: UIButton!

@IBAction func startGame() {

        firstButtonPressed()

    }

    func firstButtonPressed(){

       // var firstButton = randomNumber.randomNumberGen
        var firstButton = 0

        switch firstButton{
                    case 0:
                       orangeBtnPicked.alpha = 1
                    case 1:
                        purpleBtnPicked.alpha = 1
                    case 2:
                        redBtnPicked.alpha = 1
                    case 3:
                        blueBtnPicked.alpha = 1
                    case 4:
                        greenBtnPicked.alpha = 1
                    case 5:
                        yellowBtnPicked.alpha = 1
                    default:
                        7
                    }
         var timer = NSTimer.scheduledTimerWithTimeInterval(0.7, target: self, selector: "alphaSetting", userInfo: nil, repeats: false)
    }

    @IBAction func orangeBtnPressed(sender: UIButton) {
        buttonSender = sender
        sender.alpha = 1
        var timer = NSTimer.scheduledTimerWithTimeInterval(0.7, target: self, selector: "alphaSetting", userInfo: nil, repeats: false)
    }

   func alphaSetting(){
        if buttonSender.selected == true{
        buttonSender.alpha = 0.5
        }
        else {
          orangeBtnPicked.alpha = 0.5
        }

    }

what error are you receiving?

I might have figured it out. I took a closer look at the connection with IB and it was trying to use touch up outside instead of inside.

Taylor, I sure there is better ways to write the code than what I used but should the IBAction and IBAction work ok if they have different names? Any suggestions would be welcomed.

Thanks

Taylor, Is there anyway to see what is going on in IB behind the scenes?

Thanks

The only thing I can think of is if you right click (or ctrl click) your button, it will give you the IB connections you've made, giving you options to change them, delete them, etc. other than that I have no clue! lol

Thank you for your help.

Bryan