Swift - equivalent to IBOutletCollection

I am trying to play Stanford Matchismo from "Developing ios7 apps for iphone and ipad" in iTunesU in Swift.

On page 77 of the 3rd lecture slides , it shows the use of IBOutletCollection , which is not an option in Swift. The Swift example example shows one example with an IBOutlet array, but I can't figure out how to get Interface Builder to connect multiple outputs to the same IBOutlet / IBOutlet array.

Has anyone figured out how to do this?

I know that I can create 12 outlets and deal with them in this way, but I would like to make this work as close as possible to the example on the lecture slides.

+44
swift interface-builder iboutlet iboutletcollection
Jun 05 '14 at 5:43
source share
7 answers

EDIT

This was fixed in a later beta version of Swift - now IBCollection in the interface constructor.




For early beta versions of Swift:

I ran into the same issue: in the Beta 2 release notes you will find the following statement:

Builder interface does not support declaring alumni collections in Swift classes

I solved it as follows (easy to configure):

 class CardGameViewController: UIViewController { @lazy var cardButtons : UIButton[] = { var tempBtn: UIButton[] = [] for v:AnyObject in self.view.subviews { if v is UIButton { tempBtn.append(v as UIButton) } } return tempBtn }() ... 

Basically, it looks through all the subzones and checks to see if it is a UIButton. In this case, it is added to the temporary array. This temporary array is then used to lazily create the cardButtons array. For all details check out: Matchismo: Objective-C for Swift

+7
Jun 20 '14 at 13:28
source share

Update: Now this works correctly in Xcode - "Outlet Collection" is one of the connection options in Interface Builder that creates something similar:

 @IBOutlet var labelCollection: [UILabel]! 



While we are waiting for a fix, you can come close to this using a computable property. Say my look has five UILabels that I want in a collection. I still have to declare each, but then I also declare a computed property that collects them:

 class MyViewController { @IBOutlet var label1 : UILabel @IBOutlet var label2 : UILabel @IBOutlet var label3 : UILabel @IBOutlet var label4 : UILabel @IBOutlet var label5 : UILabel var labels: UILabel![] { return [label1, label2, label3, label4, label5] } 

The view is annoying, but from now on we can treat the labels property as if it were an IBOutletCollection , and we won’t have to change the rest of our code after fixing the error:

 override func viewDidLoad() { super.viewDidLoad() for (index, item) in enumerate(self.labels) { item.text = "Label #\(index)" } } 
+73
Jun 05 '14 at 17:34
source share

Using:

 @IBOutlet var lineFileds : [UITextField]! 

Then control the drag and drop of the TextField elements into the lineFileds in order.

+16
Apr 22 '15 at 7:15
source share
 @IBOutlet var buttons : [UIView]! 

then drag it from the connection inspector to the interface builder or some method that you usually use to do this

+11
Jun 19 '15 at 13:16
source share

I got this working in the Xcode 3 semester using this syntax

 @IBOutlet strong var views: NSArray? 

See my discussion here: stack overflow

+2
Jul 10 '14 at 21:29
source share

What @machine says seems to be the current state (Xcode 7.1) with iOS 9 bindings. The key is to put them in order. Use the first element to control + drag and drop into controller code, and then change the Outlet type to collection. Once from the controller code file, drag the output point onto each of the screen controls in order (as @machine says)

+1
Oct 30 '15 at 18:04
source share

Follow the steps to create an array of points and connect it to IB Elements:

  • Create an IBOutlets Array
  • Add multiple UIElements (Views) in your ViewController interface for the storyboard (as shown below)
  • Select ViewController (in storyboard) and Open Connector Inspector
  • In the connection inspector there is the option “Outlet Collections” (you will see an array of outlets there)
  • Connect if with interface elements

-

 class ViewController2: UIViewController { @IBOutlet var collection:[UIView]! override func viewDidLoad() { super.viewDidLoad() } } 

enter image description here

0
Oct 24 '17 at 12:10
source share



All Articles