Multiple IBOutlets in same line of same type in Swift

依然范特西╮ 提交于 2019-12-04 06:40:38

问题


In objective c you can declare IBOutlets in below mentioned manner:

IBOutlet UIButton *btn1, *btn2, *btn3;

And you can able to bind these buttons in storyboard.

Now I want to use the same terminology in Swift. I want to declare these 3 buttons in same line rather than declaring these in 3 different lines. I can be able to declare these buttons in Swift as well using:

@IBOutlet var btn1, btn2, btn3: UIButton!

But my problem is I can only able to bind "btn1" in storyboard. "btn2" & "btn3" are not showing up in the Connection Inspector.

I don't want to use UIOutletCollection class.


回答1:


You have two options to get them in a single line.

The first is to use a Referencing Outlet Collection by defining this:

@IBOutlet var fields: Array<UITextField> = []

Then link your text fields to that. You can then access them as fields[0] and fields[1] respectively.

The other option is to define them in your file like this:

@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!

Make your connections from Interface Builder, then edit the declarations to be in a single line like this:

@IBOutlet weak var emailField: UITextField!, passwordField: UITextField!

About you only options I'm afraid.



来源:https://stackoverflow.com/questions/39213467/multiple-iboutlets-in-same-line-of-same-type-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!