2010-09-18 61 views
0

我會在不同的地方爲我的視圖添加相同的UIImageViewIphone SDK:在UIIView的不同位置使用相同的UIImageView?

所以我可以做十個UIImageViews與相同的圖像,但與其他名稱。但我有約。 50 UIImageViews

我做我的UIImageView與此代碼:

代碼:

CGRect dragRect = CGRectMake(0.0f, 0.0f, 70.0, 70.0); 
dragRect.origin = CGPointMake(10, 300); 

UIImageView *image = [[UIImageView alloc] initWithFrame:dragRect]; 
[image setImage:[UIImage imageNamed:@"Apple.png"]]; 

[self.view addSubview:image; 

現在我想改變我的dracRect.origin點,並作出新UIImageView使用其他名稱。我會在for循環中執行此操作。這可能嗎?

代碼:

int x; 
for (x=0; x>=3; x=x+1) { 

    CGRect dragRect = CGRectMake(0.0f, 0.0f, 70.0, 70.0); 
    //x koordinate wird immer 10 grösser 
    dragRect.origin = CGPointMake(x*10, 300); 

    //image1, image2, image3 
    NSString *string1 = [[string1 alloc] initWithFormat:@"image%d",x]; 

    UIImageView *string1 = [[UIImageView alloc] initWithFrame:dragRect]; 
    [string1 setImage:[UIImage imageNamed:@"Apple.png"]]; 

    [self.view addSubview:string1; 
} 

更新:

image%iimage%d嘗試過,但我得到一個錯誤:

conflicting types of 'string1'

回答

0

你嘗試了嗎? 它應該工作。也許你必須在添加子視圖後釋放你的UIImageView,但我不確定。我會在CGRectMake中引入原點計算,但不應該很重要。 ,你不必寫:... initWithFormat:@「image%i」,x? 不知道,我是一個xcode noob ... :) 我做了一個類似的矩陣非iphone項目中的東西。但是我不相信在iphone sdk中製作矩陣是不可能的。

+0

首先,thx爲您的評論。我試過但我得到一個錯誤: – user451295 2010-09-18 10:24:33

+0

首先我的錯誤是:'string1'的衝突類型 – user451295 2010-09-18 10:25:42

+0

你的NSString和你的UIImageView對象具有相同的名稱!我認爲獨立於其他錯誤,這個雙重命名無法工作。嘗試使用不同的名稱。 – Micko 2010-09-18 13:02:39

相關問題