如何创建一个像我在php中使用Objective-C那样的方法

如何创建一个像我在php中使用Objective-C那样的方法,php,objective-c,ios,function,methods,Php,Objective C,Ios,Function,Methods,在PHP中,我会这样做: function changeBgColor($boxColor,$color) { echo '<div id="'. $boxColor .'">'. $color .'</div>'; } changeBgColor("box1","red"); - (void) changeBackgroundColorOfElement:(NSObject *) element toColour:(UIColor *) color {

在PHP中,我会这样做:

function changeBgColor($boxColor,$color)
{
    echo '<div id="'. $boxColor .'">'. $color .'</div>';
}

changeBgColor("box1","red");
- (void) changeBackgroundColorOfElement:(NSObject *) element toColour:(UIColor *) color {
    if ([element respondsToSelector:@selector(backgroundColor)]) [element setBackgroundColor:color];
    else if ([element respondsToSelector:@selector(tintColor)]) [element setTintColor:color];
}

以下是如何在目标C中创建函数的简要说明:

- (void) exampleFunction: (int)x y:(int)y
{
do some stuff HERE, changing BG etc
}
您可以通过以下方式调用此函数:

[self exampleFunction:*TheValueOfX* y:*TheValueOfY*]

这是创建函数并使用它的基本方法,您可能还需要了解其他更复杂的内容。

您需要编写如下方法:

function changeBgColor($boxColor,$color)
{
    echo '<div id="'. $boxColor .'">'. $color .'</div>';
}

changeBgColor("box1","red");
- (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]];
或者,您也可以使用与PHP代码调用方式相同的C方法:

void changeBackgroundColorOfElementToColour(NSObject *element, UIColor *color) {
    if ([element respondsToSelector:@selector(backgroundColor)]) [element setBackgroundColor:color];
    else if ([element respondsToSelector:@selector(tintColor)]) [element setTintColor:color];
}

谢谢你帮助我更好地理解这一点。