0

imageView1、imageView2、...という名前の9つIBOutletsがあります。imageView

配列の最初のオブジェクトをimageView1に割り当て、2番目のオブジェクトをimageView2に割り当てる必要があります.....

今私の質問は-プロパティ名を反復処理する方法はありますか

例-

 for(int i=0;i<[randomizedArray count];i++)
    {
        NSString *imageViewName=[NSString stringWithFormat:@"%@%i",@"imageView",i];
       imageViewName.image=[appDelegate.splittedImageArray ObjectAtIndex:i];
    }

つまり、値を次のように自動的に割り当てます。

imageView1.image=[appDelegate.splittedImageArray ObjectAtIndex:i];
imageView2.image=[appDelegate.splittedImageArray ObjectAtIndex:i];
4

4 に答える 4

3

IBOutlets を変更せずにそのようなものに到達できる最も近いものは次のとおりです。

NSArray *imageViews = @[imageView1,imageView2,imageView3, ...];
for(int i=0; i<[randomizedArray count]; i++) {
    [[imageViews objectAtIndex:i] setImage:[appDelegate.splittedImageArray objectAtIndex:i]];
}

しかし、より良い解決策があります -IBOutletCollection

次のようにプロパティを定義できます。

@property (nonatomic, retain) IBOutletCollection(UIImageView) NSArray *imageViews;

そして、すべての imageView を次のように接続します。

ここに画像の説明を入力

次に、すべての を含む配列が得UIImageViewられ、そのような配列を手動で宣言しなくても上記のコードを使用できます...

于 2013-06-05T05:19:42.393 に答える
0

複雑に考えないでください。これは非常に簡単です。imageview1 のタグ 1、imageview2 のタグ 2 などのように、イメージビューのタグを付けます。次に for ループを実行し、オブジェクトを選択して配列に割り当てます。それで全部です。

同様に、1 から 7 までのタグを付けたとします (どのビューにも重複したタグや 0 を付けないでください)。

NSMutableArray *arr = [[NSMutableArray alloc] init];
for(int ii = 1; ii<=7; ii++){
    UIImageView *imgView = (UIImageView *)[self.view viewWithTag:ii];
    [arr addObject:imgView];
}
yourArray = arr;
[arr release];//if your array is a property otherwise no need to release.

これがあなたを助けることを願っています。

于 2013-06-05T05:18:17.907 に答える
0

使用する NSClassFromString

 for(int i=0;i<[randomizedArray count];i++)
    {
        NSString *imageViewName=[NSString stringWithFormat:@"%@%i",@"imageView",i];
       Class theClass = NSClassFromString(imageViewName);

       theClass.image=[appDelegate.splittedImageArray ObjectAtIndex:i];
    }
于 2013-06-05T05:18:53.700 に答える
0

私がこれを行うのに使用した1つの方法。IBOutlets の配列を作成して、反復して対応する画像を割り当てることができるようにします。

for(int i=0;i<[randomizedArray count];i++)
 {
     UIImageView *imageView=[imageOutlets objectAtIndex:i];
     UIImage *image=[images objectAtIndex:i];
     imageView.image=image;
 }
于 2013-06-05T05:19:07.587 に答える