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?
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()
}
}
}
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.
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.
It is strange, I did IBOutlet with swift and it works for a while, Just realize it stop working and find out something get broke in last release of xcode beta where it is not working.