Swift ios connect multiple items to the same IBOutlet
Asked Answered
Y

6

5

I wonder if its OK to connect multiple items to the same IBOutlet? In my tableView I have setup two cells and given them a uniqe identifier.

But I have connected the label in each cell to the same IBOutlet in my custom UITableViewCell class.

enter image description here

class SearchSubCatTableViewCell: UITableViewCell {

  
    @IBOutlet weak var subCatTitle: UILabel!

So I have two labels connected to @IBOutlet weak var subCatTitle: UILabel!

This all works fine when I am testing the app but can it cause any problems?

Your answered 11/8, 2016 at 12:56 Comment(0)
C
9

Yes, this is ok as long as you don't plan on doing any operations on those labels.

The correct way to do it, is by creating an array IBOutlet:

@IBOutlet var collectionOfLabels:[UILabel]?
  • Connect all your labels to this labels array outlet.

  • Then access the labels via the array.

Cordwain answered 11/8, 2016 at 13:0 Comment(5)
What do you mean by operations? The only thing I do now is to set a titleYour
If you set the title once at the interface builder then fine, but if you need to change it later programmatically then that would cause a problem. @YourCordwain
Would the app crash? Right now one cell has a static label and the second cell has a dynamic label from JSON dataYour
Good chances that it will crash at some point, I would suggest not to take the risk and write it in a proper way.Cordwain
Is it okay if I loop through these objects, and than do something with it. Like changing the alpha?Urbanity
I
1

This may cause a problem when you will try to perform some operations on the label text data.I would suggest you to have a look at IBOutlet Collections.You can find nice tutorial here.

Importune answered 11/8, 2016 at 13:2 Comment(0)
S
1

You can connect multiple view/labels/etc to one @IBOutlet while they are from different parents.

Egzample: You have one class HeaderView with label and imageView, but you have 3 separated xib files which contains HeaderView (lets say for 3 kinds of devices (iPhone, iPhone 6Plus, and iPads). You set class of this views as HeaderView and connecting @IOBoutlets to one link.

If you want create @IBOutlet collection you have to define your outlet as array of type. For example: @IBOutlet private var labels: [UILabel]!

Sc answered 11/8, 2016 at 14:16 Comment(0)
C
0

you will not face any problem.i have done same thing many times.while creating cell each property has its own value.so i guess you will not face anyproblem

Cheffetz answered 11/8, 2016 at 13:0 Comment(0)
N
0

No, it won't cause any problems.

I used a similar setup in my app and it got accepted into the app store.

Noonday answered 11/8, 2016 at 13:1 Comment(0)
P
0

you will not face a problem if you connect multiple buttons with the same Outlet. You might face some issues if you try to perform functions on these buttons. I would suggest you to have a look at using tags if you wish to perform functions on these buttons.

Pascual answered 2/3, 2018 at 3:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.