Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 938
user2636197 Avatar asked Aug 11 '16 12:08

user2636197


2 Answers

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.

like image 196
Roy K Avatar answered Sep 24 '22 08:09

Roy K


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.

like image 35
Vishal Sonawane Avatar answered Sep 25 '22 08:09

Vishal Sonawane