2013-12-13 64 views
0

我已經被同時應用程序創建的UIImage子視圖運行將動態創建子視圖IBOutlet中

 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) 

我一直在考慮通過代碼鏈接它們,而不是在界面生成器中進行鏈接,但是至今未找到有關如何完成的任何細節。任何幫助,將不勝感激。謝謝

+0

如果我的或其他人的答案解決了您的問題,請不要忘記標記爲最佳答案,並將其提升爲使其它問題能夠解決相同問題。如果不添加評論並要求更多幫助 –

回答

0

您不能以編程方式將接口視圖與接口構建器連接起來,如果我是你,我會試着想一想解決問題的另一種方法。

+0

你有什麼建議嗎?謝謝 – user3099193

+1

爲UIImageView * newBlock創建實例變量或屬性。並在整個範圍內提及它。 –

+0

你能否進一步解釋這是如何工作的?對不起,我迄今沒有什麼經驗 – user3099193

0

您不能將它們動態連接到IBOutlets。爲什麼不將所有實例作爲變量存儲在數組中?

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

... 

[imageViews addObject: imageView]; 

... 

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