How Can I orderly arrange items in Referencing Outlet Collection automatically?

狂风中的少年 提交于 2020-01-02 06:57:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!