Carl Edwards
Carl Edwards

Reputation: 14434

Renaming an outlet in Swift

I accidentally misspelled one of the outlet's in my view controller and ran into a few issues. When I manually try to correct the typo I get stopped at runtime within AppDelegate I'm shown the message,

Thread 1: signal SIGABRT

which highlights the beginning of the code block:

class AppDelegate: UIResponder, UIApplicationDelegate {
  ...
}

I've found that to fix this issue in Objective-C you would right-click the outlet's original name and "Refactor => Rename" but unfortunately I get the message:

Xcode can only refactor C and Objective-C code

View Controller

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var billTextField: UITextField!

    @IBOutlet weak var tipRateSegmentedControl: UISegmentedControl!

    @IBOutlet weak var tiLabel: UILabel! // Variable name should be "tipLabel"

    @IBAction func calculateTapped(sender: AnyObject) {
        var userInput = billTextField.text as NSString
        var totalBill: Float = userInput.floatValue
        var index: Int = tipRateSegmentedControl.selectedSegmentIndex
        var tipRate: Float = 0.15

        if index == 0 {
            tipRate == 0.15
        } else if index == 1 {
            tipRate = 0.20
        } else {
            tipRate = 0.25
        }

        var tip: Float = totalBill * tipRate

        tipLabel.text = "$\(tip)"
    }
}

Upvotes: 19

Views: 10441

Answers (4)

Mobile Dan
Mobile Dan

Reputation: 7114

Refactor > Rename...

Xcode 9 had many editor improvements including a feature that changes an outlet/action name in the code and storyboard from one place.

In the example Swift code below, right click on btnRequestCode, from the popup menu select "Refactor > Rename...", change the outlet/action name and Xcode changes it in the Storyboard also.

@IBOutlet weak var btnRequestCode: UIButton!

enter image description here

Upvotes: 21

DMS
DMS

Reputation: 133

I like to fix situations like this by just opening the Storyboard as source code and editing the XML-like tags.

Right click the .storyboard file and choose "Open As". Search for the old outlet (or action/method) name that is still set in the storyboard but which no longer matches the renamed method/outlet object in the View Controller. Edit the Xml tag in the storyboard to give it the new name of your outlet or action in the view controller.

Upvotes: 0

Crazyrems
Crazyrems

Reputation: 2591

EDIT: Since Xcode9, there's a refactor function; the new good method is the one in this answer


Since Xcode 6, you can look for your outlet names directly in the Find navigator (Cmd+3), it shows occurrences in your code and in your xibs and storyboards. It also works with actions.

Just search the name in the Find navigator.

enter image description here

You can see that the second result is a reference in a storyboard. You can replace one by one by clicking on each result and press Replace, or you can directly Replace All if you are sure you don't break anything.

Upvotes: 24

Dániel Nagy
Dániel Nagy

Reputation: 12015

With swift this kind of refactor doesn't work yet. You have to change the name of the outlet, and then delete and reset the connection in the interface builder.

Updated: enter image description here

Right click the IBOutlet that you want to rename, delete reference outlet, and then reassign it, but you don't have to do anything with your code.

Upvotes: 7

Related Questions