How To Rename IBOutlet Variable Or IBAction Function Without Lose Connection In Swift

When i develop 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 Control key and Drag & Drop the button and the activity indicator from main storyboard view controller scene to the code assistant window, then it create two @IBOutlet variables in the ViewController class file ( ViewController.swift ).

Now i can refer the two UI components in swift source code. This is the process of connect UI components with @IBOutlet variables, you can also use this method to create an @IBAction function to process ui component event. 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 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 of 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 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.

  1. Click the @IBOutlet variable ( activityIndicator ) / @IBAction function name to select it.
  2. Right click mouse key, then click Refactor —> Rename menu item in the popup menu list.
    xcode refactor rename an iboutlet variable
  3. Now it will show below window to highlight all the place ( 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.
    use refactor rename sub menu to replace an iboutlet variable
  4. 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 name with the new variable name. Do same to rename @IBAction function name.

2.2 Find Old @IBOutlet Variable / @IBAction Function Name And Replace Them Manually.

  1. Right click the old @IBOutlet variable / @IBAction function name in ViewController.swift file.
  2. Click Find —> Find Selected Text in Workspace menu item.
  3. Then it will list all the file that contain the @IBOutlet variable / @IBAction function name text in left result panel, and the name string is highlighted. Then you can change the variable / function name manually carefully. This method is not as good as the first one.
    xcode find selected text in workspace find result list panel
0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x