xcode 在 Swift 中重命名插座
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28150826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Renaming an outlet in Swift
提问by Carl Edwards
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:
我不小心在我的视图控制器中拼错了一个插座,并遇到了一些问题。当我手动尝试更正打字错误时,AppDelegate
我在运行时停止了显示消息,Thread 1: signal SIGABRT
其中突出显示了代码块的开头:
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".
我发现要在 Objective-C 中解决这个问题,您可以右键单击插座的原始名称,Refactor => Rename
但不幸的是我收到消息:“Xcode 只能重构 C 和 Objective-C 代码”。
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)"
}
}
回答by Crazyrems
EDIT: Since Xcode9, there's a refactor function; the new good method is the one in this answer
编辑:从 Xcode9 开始,有一个重构函数;新的好方法是这个答案中的一个
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.
从 Xcode 6 开始,您可以直接在 Find navigator ( Cmd+3
) 中查找您的插座名称,它会显示您的代码以及您的 xibs 和 storyboards 中的出现次数。它也适用于动作。
Just search the name in the Find navigator.
只需在“查找”导航器中搜索名称即可。
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 Allif you are sure you don't break anything.
您可以看到第二个结果是故事板中的引用。您可以通过单击每个结果并按替换来一个一个替换,或者如果您确定没有破坏任何内容,则可以直接替换全部。
回答by Mobile Dan
Xcode 9
Xcode 9
Xcode 9 has many editor improvements including a feature that changes an outlet/action name in the code and storyboard from one place.
Xcode 9 对编辑器进行了许多改进,包括从一个位置更改代码和故事板中的插座/动作名称的功能。
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.
在下面的示例 Swift 代码中,右键单击btnRequestCode
,从弹出菜单中选择"Refactor > Rename...",更改 outlet/action 名称,Xcode 也会在 Storyboard 中更改它。
@IBOutlet weak var btnRequestCode: UIButton!
回答by Dániel Nagy
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.
使用 swift 这种重构还行不通。您必须更改插座的名称,然后在界面构建器中删除并重置连接。
Updated:
更新:
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.
右键单击要重命名的 IBOutlet,删除引用插座,然后重新分配它,但您不必对代码执行任何操作。
回答by DMS
I like to fix situations like this by just opening the Storyboard as source code and editing the XML-like tags.
我喜欢通过将 Storyboard 作为源代码打开并编辑类似 XML 的标签来解决这种情况。
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.
右键单击 .storyboard 文件并选择“打开为”。搜索仍在情节提要中设置但不再与视图控制器中重命名的方法/出口对象匹配的旧出口(或动作/方法)名称。编辑情节提要中的 Xml 标签,为它指定视图控制器中插座或动作的新名称。