2012-03-19 43 views
-4

在我的應用程序使用,我想用用名稱作爲imageView0,imageView1,imageView2循環動態創建的UIImageViews我.......添加的UIImageView動態

我的代碼如下

for(i=0;i<number;i++) 
{ 

     scrollViewImageToDrag = [[UIImageView alloc] initWithImage:image]; 
//  NSString *imageViewnameStr = [NSString stringWithFormat:@"scrollViewImageToDrag%d",i]; 

} 

for循環應該創造時代的UIImageViews與像imageView0,imageView1,imageView2系列名稱數量(例如10)執行.......

後,我怎樣才能做到這一點。

+3

如何實現什麼? – calimarkus 2012-03-19 10:05:55

+1

@AlexTerente'[UIImageView new]'與[[UIImageView alloc] init]相同' – rckoenes 2012-03-19 10:33:39

+0

只描述question_的_title_和_way被改變。基本問題仍然相同 – 2012-03-19 11:18:26

回答

0

這是不可能實現你想要的,但如果你想區分圖像瀏覽,你可以使用標籤屬性。

for (int i=0; i<[getEffectsImageData count]; i++) 
{ 
    //Your code here..... 

    imageViewsArray.tag = i ;// This would become your first image. and you can differentiate from tag. 

} 
0

與Objective-C中的許多解釋器語言不同,您不能像建議的那樣動態創建對象名稱。您可以改用數組。

NSMutableArray scrollViewImagesArray = [[NSMutableArray alloc] initWithCapacity:number]; 
for(i=0;i<number;i++) 
{ 
UIImageView *myNewImageView = [[UIImageView alloc] initWithImage:image]; 
[myImageView retain]; //With ARC you would not need this statement, you directly call this 
myImageView.tag = i; //This may come handy to identify the images if you to not want to use 
        //or do not need to. In a table view controller you may not need that array for images in a table cell. 
[scrollViewImagesArray addObject:myNewImageView]; 

} 

//以後,當你不需要的圖像的任何更多的,不要忘記刪除所有對象,並釋放阿雷(除非你使用ARC然後零分配給陣列應該做的)。從一系列

當你創建的UIImageView
4

..設置它們的標籤說(10 --- 20)

左右。 後來初始化的ImageView這樣的..

for (int i=0; i< 20; i++) { 

UIImageView *ImageView = [[UIImageView alloc] init] ; 
// ImageView formatting code here... 

ImageView.tag = i+20; 
    } 

,當你想擁有上述循環(你在你的問題了),讓你的UIImageView這樣的..

for (int i=0; i< 20; i++) { 

UIImageView *ImageView = (UIImageView *)[self.view viewWithTag:i+20]; // get the ImageView with tag 
} 

這是解決方案.. 您不能重命名文本給一個變量,並使用其功能

0
NSInteger x = y = 5; 
for(NSInteger i = 0; i < 10; i++) 
{ 
    UIImage *image = [UIIimage imageNamed:[NSString stringWithFormat:@"imageView%d",i]]; 
    UIImageView *imageView = [UIImageView alloc] init]; 
    [imageView setFrame:CGRectMake(x,y,45,45)]; 
    [imageView setImage:image]; 
    [imageView setTag:i]; 
    [yourScrollView addSubView:imageView]; 

    //If you want to access your ImageView by tapping 

    UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageSelect:)]; 
    singleTap.numberOfTapsRequired=1; 

    [imageView addGestureViewRecognizer:singleTap]; 
    image=nil; 

    if(i%4==0) { 
     x=0; 
     y=y+50; 
    } else { 
    x=x+50; 
    } 
} 

- (IBAction)imageSelect:(UITapGestureRecognizer *)sender { 
     UIImageView *imageView=(UIImageView *)sender.view; 
     //Here you can access above imageView 
} 

好運氣!