在sb中綁定了一個IBOutletCollection後,根據索引獲取元素髮現和本身拖線時的順序不一樣,有時又會根據順序,不知道是xcode的bug仍是自己就是無序的。數組
在使用的時候直接排序:xcode
- (void)viewDidLoad { [super viewDidLoad]; // Order the labels based on their y position self.labelsArray = [self.labelsArray sortedArrayUsingComparator:^NSComparisonResult(id label1, id label2) { if ([label1 frame].origin.y < [label2 frame].origin.y) return NSOrderedAscending; else if ([label1 frame].origin.y > [label2 frame].origin.y) return NSOrderedDescending; else return NSOrderedSame; }]; }
把數組進行排序:
spa
@interface NSArray (sortBy) - (NSArray*) sortByObjectTag; - (NSArray*) sortByUIViewOriginX; - (NSArray*) sortByUIViewOriginY; @end
而後包含頭文件,你選擇了它,該代碼能夠是:@implementation NSArray (sortBy) - (NSArray*) sortByObjectTag { return [self sortedArrayUsingComparator:^NSComparisonResult(id objA, id objB){ return( ([objA tag] < [objB tag]) ? NSOrderedAscending : ([objA tag] > [objB tag]) ? NSOrderedDescending : NSOrderedSame); }]; } - (NSArray*) sortByUIViewOriginX { return [self sortedArrayUsingComparator:^NSComparisonResult(id objA, id objB){ return( ([objA frame].origin.x < [objB frame].origin.x) ? NSOrderedAscending : ([objA frame].origin.x > [objB frame].origin.x) ? NSOrderedDescending : NSOrderedSame); }]; } - (NSArray*) sortByUIViewOriginY { return [self sortedArrayUsingComparator:^NSComparisonResult(id objA, id objB){ return( ([objA frame].origin.y < [objB frame].origin.y) ? NSOrderedAscending : ([objA frame].origin.y > [objB frame].origin.y) ? NSOrderedDescending : NSOrderedSame); }]; } @end
- (void)viewDidLoad { [super viewDidLoad]; // Order the labels based on their y position self.labelsArray = [self.labelsArray sortByUIViewOriginY]; }