user2636197
user2636197

Reputation: 4122

Swift ios connect multiple items to the same IBOutlet

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?

Upvotes: 5

Views: 5517

Answers (6)

alchemist101
alchemist101

Reputation: 1

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.

Upvotes: 0

Daniel Sumara
Daniel Sumara

Reputation: 2264

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]!

Upvotes: 1

Roy K
Roy K

Reputation: 3319

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.

Upvotes: 9

Vishal Sonawane
Vishal Sonawane

Reputation: 2693

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.

Upvotes: 1

NiCk.JaY
NiCk.JaY

Reputation: 141

No, it won't cause any problems.

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

Upvotes: 0

Prashant Ghimire
Prashant Ghimire

Reputation: 548

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

Upvotes: 0

Related Questions