Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ios - Is IBOutletCollection guaranteed to be of correct order?

I have two contradicting sources in regards to IBOutletCollection with NSArray. First says that the order that you link things from interface to outlet collection is significant, and it will always be the same in outlet collection. E.g. if we link first text field first to collection, element number 0 will be that field, etc.

But another source tells me that this is wrong and actually Outlet collection is randomized and there is absolutely no guarantee in the order of things. So element number zero can be any text field, and not just the first connected in interface.

"iOS 6 Programming - exploring iOS SDK" is the first source and Stanford course on iTunes is the second source. Who is right?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Simply assign the order of controls by tag and on load reorder them.


property observing:

@IBOutlet var btnCollection: [UIButton]! {
    didSet {
        btnCollection.sort { $0.tag < $1.tag }
    }
}

in viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()
    btnCollection = btnCollection.sorted { $0.tag < $1.tag }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...