Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Mike Kohl
PLUS
Mike Kohl
Courses Plus Student 6,793 Points

unrecognized selector

Crystal ball application video: buttons and IBaction

When creating an IBAction for the predict button. My code was different than the video.

video showed: -(IBAction)buttonPressed:(id)sender;

my code: -(IBAction)buttonPressed:(UIButton *)sender;

I figured I did something wrong, so I deleted my code from the .h and .m file. Added the code again, ran the application in the simulator, and when I pressed the predict button the program fails.

I, also, tried doing a clean build (command-option-shift-k), that did not work. I deleted the code from the .h and .m file again. Ran the program, clicked on the predict button and still same error.

alt text

Hi. The button action is connected to the method buttonPressed. When you click on the button the app is calling buttonPressed and because you it is missing the app crashes with the error unrecognized selector sent to the the instance btnPressed: To fix this you have to implement bthPressed in your code. Your code and the video is basically the same.
Video code: -(IBAction)buttonPressed:(id)sender; my code: -(IBAction)buttonPressed:(UIButton *)sender;

The difference is in the video the variable sender is type id which means an unknown type or any type and in your code you specify that the variable sender is type UIButton Both will work correctly. The difference would be if in the code you want to access properties of the sender the xcode will be able to autocomplete because it knows that is an UIButton. For example you can do button.TitleLabel.Text = "button";

I hope that helps. Post back if you still getting an error.

Mike Kohl
Mike Kohl
Courses Plus Student 6,793 Points

When I first create the application, drag the button over to the storyboard, and then run the app. I click on the button, and I do not get the error message. No error. This is what I expected to happen when I removed my (IBAction) buttonProcessed method from the .h and .m files.

For example: Microsoft's C#

If I drag a button to the form and create a click event. Visual Studio generates EventHandlers for me.

If I delete the click event, then compile the program, I would receive an error. I could then just delete the EventHandler that visual studio created, recompile the error would go away.

Is this a similar situation? Where xcode is creating some kind of connection, that I can just delete and the error would go away? I deleted the code I generated, but I am not sure why the error is occurring when the code is gone.

I did figure out that I could just delete the button off the storyboard and that would solve any issue I had. But I really want to know why do I get this error. .when the method is gone.. where is this connection at that tells xcode.. hey he is missing the buttonPressed event.

1 Answer

Mike Kohl
PLUS
Mike Kohl
Courses Plus Student 6,793 Points

I found the solution to my problem.

Problem: " I deleted my code but not the connection points. "

If I select my button then in the Utilities panel, clicked on the icon "Show the Connections Inspector", and scroll down to "Sent Events" section, I could see that the View Controller "buttonPressed" was mapped to "Touch Up Inside". Even though I deleted the code for my buttonPressed action, the connection still existed. All I had to do is click the "x" next to the view controller "buttonPressed", which removed the connection.

I ran my program again, and there were no errors.

Great! That is true Xcode will not delete the connection if you delete the method that it is connected to. You have to do it manually.