Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't hook up an outlet collection in Xcode 6 using storyboard

Tags:

swift

xcode6

I am having trouble creating an outlet collection in Xcode 6. Outlet collections in Xcode 6 now function as regular IBOutlets, and you use the same @IBOutlet attribute to declare an outlet collection while being sure to specify an array for the type. I have done this in my view controller's swift file i.e.

@IBOutlet var cardButtons: UIButton[]

In Xcode 5 when one control drags from an element in the storyboard to the corresponding view controller using the assistant editor they are presented with an option to create either an outlet or an outlet collection. This seems to no longer be possible in Xcode 6 and my guess is because outlets and outlet collection now share the same @IBOutlet attribute. How should I create an outlet collection which will contain say 10 buttons without being able to control drag each one from the storyboard view and hook it up to my

@IBOutlet var cardButtons: UIButton[] 

property in my view controller swift file?

like image 927
sheltereddougie Avatar asked Jun 05 '14 17:06

sheltereddougie


People also ask

What is outlet in Swift?

An outlet is a property that is annotated with the symbol IBOutlet and whose value you can set graphically in a nib file or a storyboard. You declare an outlet in the interface of a class, and you make a connection between the outlet and another object in the nib file or storyboard.


3 Answers

You've got it right, you just need to define the array more formally:

@IBOutlet var cardButtons: Array<UIButton>

Now you'll be able to connect the buttons from IB.


The above should work, but still doesn't in Xcode 6 beta 3. A workaround is to use an NSArray until Xcode and Swift can handle this properly:

class ViewController: UIViewController {
    @IBOutlet strong var labels: NSArray!

    override func viewDidLoad() {
        super.viewDidLoad()

        for label in self.labels as [UILabel] {
            label.textColor = UIColor.redColor()
        }
    }
}
like image 55
Nate Cook Avatar answered Sep 22 '22 07:09

Nate Cook


This is under the Xcode 6 beta known issues: "Interface Builder does not support declaring outlet collections in Swift classes. (15607242)"

Nate Cook's answer is correct for attaching outlets, but not outlet collections. Hopefully in the next Xcode 6 beta release this issue will be resolved.

like image 37
CodyMace Avatar answered Sep 24 '22 07:09

CodyMace


In seed 3 of Xcode 6, the following syntax works:

@IBOutlet strong var cardButtons: NSArray?

Note the following:

  • You have to use strong because @IBOutlet is weak by default, and since the array is not in the interface, it will vanish before you have a chance to use it.

  • You have to use NSArray because you can't mark Array as strong.

Knowing the contained type is now up to you, of course.

Note also that this is not the syntax advertised by the docs or by Xcode itself when you control-drag to form an outlet collection. I can't help that; using that syntax causes a seg fault, so clearly something else is needed, at least for now.

like image 25
matt Avatar answered Sep 22 '22 07:09

matt