0

我正在使用以下代码为一系列视图设置数组。bunnyView1 到 bunnyView7 是在情节提要中创建的 UIImageView 的实例,但我想自动化该过程,以便我可以使用代码生成连接。我将如何设置循环内的值?

- (void)viewDidLoad
{   
NSMutableArray *hopAnimation = [[NSMutableArray alloc] initWithCapacity:20];
for (int i = 1; i <= 20; i++) {
    [hopAnimation addObject:[UIImage imageNamed:[NSString stringWithFormat:@"frame-%i.png", i]]];
}
self.bunnyView1.animationImages=hopAnimation;
self.bunnyView2.animationImages=hopAnimation;
self.bunnyView3.animationImages=hopAnimation;
self.bunnyView4.animationImages=hopAnimation;
self.bunnyView5.animationImages=hopAnimation;
self.bunnyView6.animationImages=hopAnimation;
self.bunnyView7.animationImages=hopAnimation;
self.bunnyView1.animationDuration=1;
self.bunnyView2.animationDuration=1;
self.bunnyView3.animationDuration=1;
self.bunnyView4.animationDuration=1;
self.bunnyView5.animationDuration=1;
self.bunnyView6.animationDuration=1;
self.bunnyView7.animationDuration=1;                             
[super viewDidLoad];

}

4

2 回答 2

1

将变量添加到数组中。并迭代数组并设置值。例如,

NSArray *collection = [[NSArray    alloc]initWithObjects:@"object1",@"object2",@"object3",@"object4",nil];
于 2012-05-14T13:10:38.837 回答
1

您应该使用每个 bunnyView 都应该链接到的 UIImageView 的 IBOutletCollection。声明是这样的:

@property (nonatomic,retain) IBOutletCollection(UIImageView) NSArray * bunnyViews;
于 2012-05-14T14:06:51.257 回答