When hooking Swift code up to a Storyboard, how do you add the IBAction
and IBOutlet
tags?
当将Swift代码连接到故事板时,如何添加IBAction和IBOutlet标记?
5 个解决方案
#1
31
Add IBAction
and IBOutlet
attributes to variables and functions so they can be visible in Interface builder.
向变量和函数添加IBAction和IBOutlet属性,以便它们在接口构建器中可见。
class ViewController: UIViewController {
@IBOutlet var label: UILabel?
@IBAction func doTap(x:UIButton) {
println("Tapped: \(x)")
}
}
#2
4
Below code shows IBOutlet and IBAction format in Swift :
以下代码显示了Swift中的IBOutlet和IBAction格式:
class MyViewController: UIViewController {
@IBOutlet weak var btnSomeButton: UIButton?
@IBOutlet weak var lblLabelItem: UILabel?
@IBAction func btnSomeButtonClicked(sender: UIButton) {
...
}
}
You can bind them same way as done in Objective-C.
你可以像Objective-C那样绑定它们。
#3
3
Just use old ctrl + drag technique which was popular in Xcode5 and everything works fine.
使用旧的ctrl +拖动技术,这在Xcode5中很流行,一切都很好。
#4
3
I would agree more with Jayprakash than the upvoted first answer. The only thing I would correct is the marking of the IBOutlets as implicitly unwrapped with the !
The first answer used to be correct, but several changes were made in Swift and how it interacts with IB in the latest release. In Swift, IBOutlets no longer have any implicit behavior or magic--they are simply annotations for IB. As of the date of this response, the following code is correct:
我更同意Jayprakash,而不是投票支持的第一个答案。我唯一要纠正的是,iboutlet的标记是隐式展开的!第一个答案曾经是正确的,但是在Swift中做了一些更改,以及在最新版本中如何与IB交互。在Swift中,IBOutlets不再有任何隐含的行为或魔法——它们只是IB的注解。
// How to specify an the equivalent of IBOutletCollection in Swift
@IBOutlet var fields: [UITextField]!
// How to specify a standard IBOutlet
@IBOutlet weak var button: UIButton!
// How to specify an IBAction
@IBAction func buttonWasPressed(sender: UIButton) { ... }
#5
1
While creating a project, you should have selected the storyboard, so that you can add your IBOutlet
's directly in the story board.
在创建项目时,您应该选择故事板,这样您就可以在故事板中直接添加IBOutlet。
The Below code gives you a idea of how to add IBOutlet
to the UILabel
下面的代码告诉您如何将IBOutlet添加到UILabel中
class ViewController: UIViewController {
@IBOutlet var label : UILabel
}
#1
31
Add IBAction
and IBOutlet
attributes to variables and functions so they can be visible in Interface builder.
向变量和函数添加IBAction和IBOutlet属性,以便它们在接口构建器中可见。
class ViewController: UIViewController {
@IBOutlet var label: UILabel?
@IBAction func doTap(x:UIButton) {
println("Tapped: \(x)")
}
}
#2
4
Below code shows IBOutlet and IBAction format in Swift :
以下代码显示了Swift中的IBOutlet和IBAction格式:
class MyViewController: UIViewController {
@IBOutlet weak var btnSomeButton: UIButton?
@IBOutlet weak var lblLabelItem: UILabel?
@IBAction func btnSomeButtonClicked(sender: UIButton) {
...
}
}
You can bind them same way as done in Objective-C.
你可以像Objective-C那样绑定它们。
#3
3
Just use old ctrl + drag technique which was popular in Xcode5 and everything works fine.
使用旧的ctrl +拖动技术,这在Xcode5中很流行,一切都很好。
#4
3
I would agree more with Jayprakash than the upvoted first answer. The only thing I would correct is the marking of the IBOutlets as implicitly unwrapped with the !
The first answer used to be correct, but several changes were made in Swift and how it interacts with IB in the latest release. In Swift, IBOutlets no longer have any implicit behavior or magic--they are simply annotations for IB. As of the date of this response, the following code is correct:
我更同意Jayprakash,而不是投票支持的第一个答案。我唯一要纠正的是,iboutlet的标记是隐式展开的!第一个答案曾经是正确的,但是在Swift中做了一些更改,以及在最新版本中如何与IB交互。在Swift中,IBOutlets不再有任何隐含的行为或魔法——它们只是IB的注解。
// How to specify an the equivalent of IBOutletCollection in Swift
@IBOutlet var fields: [UITextField]!
// How to specify a standard IBOutlet
@IBOutlet weak var button: UIButton!
// How to specify an IBAction
@IBAction func buttonWasPressed(sender: UIButton) { ... }
#5
1
While creating a project, you should have selected the storyboard, so that you can add your IBOutlet
's directly in the story board.
在创建项目时,您应该选择故事板,这样您就可以在故事板中直接添加IBOutlet。
The Below code gives you a idea of how to add IBOutlet
to the UILabel
下面的代码告诉您如何将IBOutlet添加到UILabel中
class ViewController: UIViewController {
@IBOutlet var label : UILabel
}