php - 如何创建一个方法,就像我在 php 中所做的那样,但在 Objective-C 中

标签 php objective-c ios function methods

在 PHP 中我会做这样的事情:

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

changeBgColor("box1","red");

但是我还没有找到在 Objective-C 中做到这一点的方法

我想用 Objective-C 编写一些代码,并能够调用一个方法来执行该代码。

例如执行这段代码。但是我想在调用方法时更改 boxColor 和 redColor

boxColor.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]];

或者您可以使用与 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];
}

关于php - 如何创建一个方法,就像我在 php 中所做的那样,但在 Objective-C 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7707203/

相关文章:

php - 显示与 id 相关的结果并根据角色分类 - Laravel

objective-c - 安全的 __attribute__((constructor)) 函数类

php - URL 的 Base64_encode

php - 在 Yii CDbCriteria 语句中使用 CASE

php - 在 Yii 中使用元素名称创建 URL #

ios - 调用一个函数,该函数将在函数指针上启动 NSThread

php - Instagram API - 无法获取已接受关注请求的私有(private)用户的媒体

ios - Entitlements.plist 对于 Xcode 4 中的分发仍然是必需的吗?

ios - 如何通过 UIButton 变量调用操作

ios - 当我为 AFNetworking 设置我的 downloadProgressBlock 时,它只会在最后调用一次