问题
I have Referencing Outlet Collection of UITextfields arranged in Xib as one below another. When I print Referencing Outlet Collection I found that its orderless. I need to automatically arrange text fields, ie text fileld comes 1st in UI should appear 1st in Outlet Collection Array. Any idea?
回答1:
This is an unfortunate fact of IBOutletCollection
. You may want to dupe rdar://12121242.
You can set a tag for each view in IB, and then sort the collection by tag
. You can also sort the collection by frame origin. In either case, you will have to do this by hand in viewDidLoad
.
回答2:
Assuming you have an IBOutletCollection called labelsArray, and you have set the tag property of each to the desired order, then the code is:
-(void)viewDidLoad {
self.labelsArray = [self.labelsArray sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"tag" ascending:YES]]];
}
回答3:
Yes, apple has provided facility for this by help of IBOutletCollection declare property like this
@property (nonatomic,retain) IBOutletCollection(UILabel) NSArray *labelsArray;
Connect all controls with above property.
labelsArray automatically instantiated for you when the NIB file is loaded
Now just use labelsArray in your program for oulets. for maintaining order set the tag value of controls as you wish
来源:https://stackoverflow.com/questions/14848599/how-can-i-orderly-arrange-items-in-referencing-outlet-collection-automatically