LoginSignup
6
6

More than 5 years have passed since last update.

IBOutletCollectionの順序性が無い問題の解決を図りました

Last updated at Posted at 2012-04-29

複数のUIButtonやUILabelをIBOutletCollectionに
まとめると最初からNSArrayに入っているので使い勝手がよいはず
なのですが順序がグチャグチャになってしまう欠点があります。

それで解決方法です。


@interface UIControl (sorted)
- (NSComparisonResult)compareByY:(UIControl*)control;
@end
@implementation UIControl (sorted)

- (NSComparisonResult)compareByY:(UIControl*)control
{
    NSComparisonResult result;
    if (self.frame.origin.y <control.frame.origin.y) {
        result=NSOrderedAscending;
    }else if(self.frame.origin.y > control.frame.origin.y){
        result=NSOrderedDescending;
    }else {
        result=NSOrderedSame;
    }
    return result;
}
@end

上記のようにカテゴリでソート用比較メソッドを追加し


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.buttonAnswers=[self.buttonAnswers sortedArrayUsingSelector:@selector(compareByY:)];


}

UIViewContorollerの実装内でソートをかけるわけです。
配置座標のY値でソートしています。
だいたい並び順でソートしたいというパターンだと思いますので。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6