2012-06-28 49 views
-1

對於以下代碼示例,通常我會使用[self fall];而不是*處的代碼,但我還需要將i的值發送到fall方法。我該怎麼做呢?具有特定值的調用方法

- (void)main { 
    for (int i=0; i <= 100; i++) { 
     [image[i] fall]; * 
    } 
} 

- (void)fall { 
    // manipulate image[i]; separately from the for loop 
} 

編輯:我會接受最古老的答案,因爲所有都是正確的。謝謝!

回答

3

你需要做的 -

- (void)fall:(int)i { 
    // manipulate image[i]; separately from the for loop 
} 

,並呼籲像 -

- (void)main { 
for (int i=0; i <= 100; i++) { 
    [image fall:i]; 
} 

}

編輯 -

如果你想通過指數 -

- (void)fall:(int)i { 
    // manipulate image[i]; separately from the for loop 
} 

,並呼籲像 -

- (void)main { 
for (int i=0; i <= 100; i++) { 
    [self fall:i]; // Now from here you can either pass index 
} 

}

如果你想通過一些圖片 -

- (void)fall:(UIImage)i { 
    // manipulate image[i]; separately from the for loop 
} 

,並呼籲像 -

- (void)main { 
for (int i=0; i <= 100; i++) { 
    [self fall:imageI]; // Now from here you need to pass image, if that image is stored in array, then fetch from array. Or you need to manipulate in the way in which you are storing. 
} 

}

+0

嘿,當我這樣做時,我得到的警告「'UIImageView'可能不會響應'秋天:'和應用程序凍結循環運行時。有任何想法嗎? –

+1

是的,它會的。因爲方法是視圖控制器,如果你用圖像對象調用它,它肯定會失敗。早些時候我認爲圖像是一些控制器名稱。檢查已編輯的帖子。 – rishi

3

也許你的意思是:

- (void)main { 
    for (int i=0; i <= 100; i++) { 
     [image[i] fall:i]; 
    } 
} 

- (void)fall:(int)i { 
    // manipulate image[i]; separately from the for loop 
} 

或者,也許你的意思是:

- (void)main { 
    for (int i=0; i <= 100; i++) { 
     [self fall:image[i]]; 
    } 
} 

- (void)fall:(NSImage *)image { 
    // manipulate image[i]; separately from the for loop 
} 

如果沒有,你需要澄清你的問題。

+0

嘿,當我這樣做時,我得到的警告''UIImageView'可能不會響應'秋天:'「和應用程序凍結循環運行時。有任何想法嗎? –