Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you share an outlet between two views?

Tags:

xcode

swift

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

like image 744
Martin Muldoon Avatar asked Nov 10 '15 16:11

Martin Muldoon


2 Answers

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.

like image 79
dudeman Avatar answered Oct 12 '22 18:10

dudeman


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.

like image 30
Mr Beardsley Avatar answered Oct 12 '22 17:10

Mr Beardsley