将动态创建的子视图连接到IBOutlet

时间:2013-12-13 12:01:05

标签: ios objective-c subview

我有在App运行时创建的UIImage子视图

     UIImageView *newBlock = [[UIImageView alloc] initWithImage:
                             [UIImage imageNamed:@"image.png"]];
    newBlock.frame = CGRectMake(0.0, 0.0, 58.0, 58.0);
    self.view.userInteractionEnabled = YES;
    [self.view addSubview:newBlock];

然而,因为它们在Interface Builder中不存在(或者至少我找不到它们),我无法将我的IBOutlet连接到新图像,因此它们基于计时器移动

 timer = [NSTimer scheduledTimerWithTimeInterval:(0.001) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
pos = CGPointMake(0.0,0.05);

...

- (void) onTimer {

blocks.center = CGPointMake(blocks.center.x+pos.x,blocks.center.y+pos.y)

我一直在考虑通过代码链接它们的方法,而不是在Interface Builder中,但是迄今为止还没有找到任何有关如何完成此操作的细节。任何帮助,将不胜感激。感谢

2 个答案:

答案 0 :(得分:0)

您无法以编程方式将视图与界面构建器连接,如果我是您,我会尝试考虑针对您的问题采用不同的方法。

答案 1 :(得分:0)

您无法将它们动态连接到IBOutlets。为什么不简单地将数组中的所有实例存储为变量?

NSMutableArray *imageViews = [[NSMutableArray alloc] initWithCapacity: X];

...

[imageViews addObject: imageView];

...

for(UIView *view in imageViews)
{
   ...
}