Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is IBOutletCollection guaranteed to be of correct order?

Tags:

ios

ios6

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?

like image 674
Dvole Avatar asked Mar 03 '13 19:03

Dvole


1 Answers

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 } } 
like image 94
Darshit Shah Avatar answered Sep 26 '22 04:09

Darshit Shah