When I develop the iOS app user interface, I add one button and one activity indicator in the main storyboard file. Because I want to write swift source code to manage the button and the activity indicator, so I press the Control key and Drag & Drop the button and the activity indicator from the main storyboard view controller scene to the code assistant window, then it creates two @IBOutlet variables in the ViewController class file ( ViewController.swift ).
Now I can refer to the two UI components in the Swift source code. This is the process of connecting UI components with @IBOutlet variables, you can also use this method to create an @IBAction function to process UI component events. Please refer article How To Connect Storyboard UI Component To ViewController Class Code to learn more.
1. This Class Is Not Key-Value Coding-Compliant For The Key ActivityIndicator Error.
- But sometime after I rename an @IBOutlet variable or @IBAction function in the ViewController class source code, when I run the app, it will throw an error message like below.
019-12-18 09:07:18.441998+0800 iOSActivityIndicatorExample[1319:21404] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<iOSActivityIndicatorExample.ViewController 0x7f9dfe40c990> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key activityIndicator.'
- This is because the @IBOutlet variable / @IBAction function’s name is changed, but the old @IBOutlet variable / @IBAction function name is used in a lot of places in the Xcode project files. So the connection between the UI component and the @IBOutlet variable / @IBAction function has been lost.
2. How To Rename An @IBOutlet Variable / @IBAction Function Correctly To Avoid Lose Connection Error.
There are two methods for you to rename an @IBOutlet variable / @IBAction function correctly in Xcode.
2.1 Use Refactor —> Rename Menu Item.
- Click the @IBOutlet variable ( activityIndicator ) / @IBAction function name to select it in swift source code.
- Right-click the mouse key, then click the menu item Refactor —> Rename in the popup menu list.
- Now it will show a window to highlight all the places ( project files ) that this @IBOutlet variable / @IBAction function name is used. In this example, the old variable name ( activityIndicator ) is only used in Main.storyboard file.
- Now change the highlighted old variable name activityIndicator to activityIndicator1 in the window, and click Rename button ( a blue button at the rename window top right corner ), then it will replace all the old variable names with the new variable name. Do the same to rename the @IBAction function name.
2.2 Find Old @IBOutlet Variable / @IBAction Function Name And Replace Them Manually.
- Right click the old @IBOutlet variable / @IBAction function name in ViewController.swift file.
- Click Find —> Find Selected Text in Workspace menu item.
- Then it will list all the files that contain the @IBOutlet variable / @IBAction function name text in the left result panel, and the name string is highlighted. Then you can change the variable or function name manually carefully. This method is not as good as the first one.