Martin Muldoon
Martin Muldoon

Reputation: 3428

Can you share an outlet between two views?

I have a view controller and two views. I have an outlet called name. I connect the outlet to a label on the first view and the value appears fine. I have another label on the second view. When I connect it to the name outlet, the connection on the first view is broken.

Any thought?

Thanks

Martin

Upvotes: 6

Views: 2379

Answers (2)

dudeman
dudeman

Reputation: 1136

You can't attach more than one label to an outlet, but you can attach more than one label to an outlet collection. It's the same idea as an outlet, but instead of having one label (or view) you have an array. The procedure is the same for creating regular outlets, but you just drag from the section in the Connections Inspector under "Referencing Outlet Collection". The first time you do that, an array will be created to manage your outlet collection. Then, every time after that you just connect your other labels (or views) to that same collection.

Upvotes: 7

Mr Beardsley
Mr Beardsley

Reputation: 3863

No, you cannot connect multiple objects to a single IBOutlet. The right way to handle 2 different objects is to create two outlets in your view controller

@IBOutlet weak var nameInView1: UILabel!
@IBOutlet weak var nameInView2: UILabel!

And connect each outlet to the appropriate label.

Upvotes: 2

Related Questions