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?

Community
  • 1
  • 1
user2636197
  • 3,982
  • 9
  • 48
  • 69

6 Answers6

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.

Roy K
  • 3,319
  • 2
  • 27
  • 43
  • What do you mean by operations? The only thing I do now is to set a title – user2636197 Aug 11 '16 at 15:36
  • 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. @user2636197 – Roy K Aug 11 '16 at 15:37
  • Would the app crash? Right now one cell has a static label and the second cell has a dynamic label from JSON data – user2636197 Aug 11 '16 at 16:23
  • Good chances that it will crash at some point, I would suggest not to take the risk and write it in a proper way. – Roy K Aug 11 '16 at 16:24
  • Is it okay if I loop through these objects, and than do something with it. Like changing the alpha? – Stijn Westerhof Jan 31 '17 at 11:53
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.

Vishal Sonawane
  • 2,637
  • 2
  • 16
  • 21
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]!

Daniel Sumara
  • 2,234
  • 20
  • 25
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

Prashant Ghimire
  • 518
  • 4
  • 20
0

No, it won't cause any problems.

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

NiCk.JaY
  • 141
  • 1
  • 8
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.