使用特定值调用方法

时间:2012-06-28 15:44:33

标签: iphone objective-c ios cocoa-touch sdk

对于下面的代码示例,通常我会使用[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
}

编辑:我会接受最老的答案,因为一切都是正确的。谢谢!

2 个答案:

答案 0 :(得分: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
}

如果没有,您需要澄清您的问题。

答案 1 :(得分: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.
}

}