Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
777 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...