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 make my iOS app stop at the first line of class AppDelegate, i know AppDelegate class is the enter 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 do not continue, it stop here and the app hangs. When you click 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 time.
So i think maybe there are some exception occurred. I follow article How To Debug And Get Error Messages In Xcode Step By Step to find out error message 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 investigate, 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 any more. But the connection between the button and the removed function still exist. So i need to remove the connection between the ui button and the removed function also. Please follow below steps to find out the connections between button and related functions.
- Click Show the Project navigator button ( the folder button at the left most ) in Xcode left panel to display all project files.
- Click Main.storyboard file to open it in 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 has been connected to the Sent Events —> Touch Up Inside event. You should click the close button at the unused function left side to remove the connection.
- Now rebuild your Xcode project, and the error should be fixed.
- But if you find above method do not take effect, and the error still exist, 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 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