我使用以下代码为一系列视图设置数组。 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];
}
答案 0 :(得分:1)
在数组中添加变量。并迭代数组并设置值。例如,
NSArray *collection = [[NSArray alloc]initWithObjects:@"object1",@"object2",@"object3",@"object4",nil];
答案 1 :(得分:1)
你应该使用UIImageView的IBOutletCollection,每个bunnyView都应链接到它。宣言如下:
@property (nonatomic,retain) IBOutletCollection(UIImageView) NSArray * bunnyViews;