When I develop an iOS example application use swift, I meet a strange situation. When I click the run button to start my iOS example, it runs well. My example iOS app has a button, so I click it to trigger a Touch Up Inside event for this button, but this action makes my iOS app stop at the first line of class AppDelegate, I know AppDelegate class is the entry point for every iOS app, so I just click the Step over button to continue to run the example.
But I found the swift code execution does not continue, it stops here and the app hangs. When you click the Step over or Continue program execution button, the program execution will return to the first line of AppDelegate class, and the event process function code will not be invoked. This confused me a lot of times.
So I think maybe there are some exception occurred. I follow the article How To Debug And Get Error Messages In Xcode Step By Step to find out the error messages like this unrecognized selector sent to instance. Below is the full error messages stack trace.
2019-06-27 14:37:30.286038+0800 ManageSubViewExample[1443:29617] -[ManageSubViewExample.ViewController changeSubViewTransparence:]: unrecognized selector sent to instance 0x7fb7b9d0c880 2019-06-27 14:37:30.297875+0800 ManageSubViewExample[1443:29617] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ManageSubViewExample.ViewController changeSubViewTransparence:]: unrecognized selector sent to instance 0x7fb7b9d0c880' *** First throw call stack:
1. How To Fix Unrecognized Selector Sent To Instance Error When Click Button.
- After investigating, I finally find out the reason.
- This is because I had connected two functions to the button’s Touch Up Inside event and I remove one old function source code from ViewController.swift file because I do not use it anymore.
- But the connection between the button and the removed function still exists. So I need to remove the connection between the UI button and the removed function also.
- Please follow the below steps to find out the connections between the button and related functions.
- Click Show the Project navigator button ( the folder button at the leftmost ) in Xcode left panel to display all project files.
- Click Main.storyboard file to open it in the designer.
- Select the UI button component in the designer.
- Then click View —> Inspectors —> Show Connections Inspector menu item at top menu bar. This will open the connections inspector window for the button at Xcode right panel.
- You can see there are two functions that have been connected to the Sent Events —> Touch Up Inside event. You should click the close button on the unused function left side to remove the connection.
- Now rebuild your Xcode project, and the error should be fixed.
- But if you find the above method does not take effect, and the error still exists, then remove all the two connections for the button, and reconnect the UI button with the correct function again, then the error should be fixed. You can read the article How To Connect Storyboard UI Component To ViewController Class Code to learn more.
- iOS Add Click Event To UIButton Swift Example
- How To Display Xcode Library, Assistant Editor, Inspectors Window