在PHP中,我将这样做:
function changeBgColor($boxColor,$color)
{
echo '<div id="'. $boxColor .'">'. $color .'</div>';
}
changeBgColor("box1","red");
但是我还没有找到在Objective-C中做到这一点的方法
我想在Objective-C中编写一些代码,并能够调用一个方法来执行这些代码。
。执行这段代码。但是我想在调用
方法时改变boxColor和redColorboxColor.backgroundColor = [UIColor redColor];
您需要编写这样一个方法:
- (void) changeBackgroundColorOfElement:(NSObject *) element toColour:(UIColor *) color {
if ([element respondsToSelector:@selector(backgroundColor)]) [element setBackgroundColor:color];
else if ([element respondsToSelector:@selector(tintColor)]) [element setTintColor:color];
}
可以被调用:
[self changeBackgroundColorOfElement:myLabel toColour:[UIColor blackColor]];
或者你也可以用C的方法像PHP代码那样调用
void changeBackgroundColorOfElementToColour(NSObject *element, UIColor *color) {
if ([element respondsToSelector:@selector(backgroundColor)]) [element setBackgroundColor:color];
else if ([element respondsToSelector:@selector(tintColor)]) [element setTintColor:color];
}
下面是关于如何在objective C中创建函数的简要说明:
- (void) exampleFunction: (int)x y:(int)y
{
do some stuff HERE, changing BG etc
}
你可以调用这个函数:
[self exampleFunction:*TheValueOfX* y:*TheValueOfY*]
这是创建和使用函数的基本方法,还有其他更复杂的东西,你可能想要阅读