Multiple IBOutlets in same line of same type in Sw

2020-05-02 13:43发布

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条回答
祖国的老花朵
2楼-- · 2020-05-02 14:22

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.

enter image description here

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.

查看更多
登录 后发表回答