dousuo2812 2011-10-09 23:11
浏览 31
已采纳

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

In PHP I would do something like this:

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

changeBgColor("box1","red");

But I haven't found a way to do this in Objective-C

I want to write out some code in Objective-C and be able to call a method to execute that code.

e.g. execute this code. But I want to change the boxColor and redColor when I call the method

boxColor.backgroundColor = [UIColor redColor]; 
  • 写回答

2条回答 默认 最新

  • du8442 2011-10-10 00:25
    关注

    You need to write a method like this:

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

    that can be called by:

    [self changeBackgroundColorOfElement:myLabel toColour:[UIColor blackColor]];
    

    Or you can have the C method which you would call the same way as your PHP code:

    void changeBackgroundColorOfElementToColour(NSObject *element, UIColor *color) {
        if ([element respondsToSelector:@selector(backgroundColor)]) [element setBackgroundColor:color];
        else if ([element respondsToSelector:@selector(tintColor)]) [element setTintColor:color];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在hololens1上运行unity项目只有空窗口
  • ¥25 TABLEAU PREP无法打开
  • ¥15 关于#c语言#的问题:求完整代码条件好说
  • ¥15 (需要远程,AI不回)VB6二进制文件转换成功,但是C#转换总是失败
  • ¥15 关于#matlab#的问题:有没有什么其他办法能够保证不退出进程(相关搜索:matlab调用)
  • ¥15 依据报错在原代吗格式的基础上解决问题
  • ¥15 在虚拟机中安装flash code
  • ¥15 单片机stm32f10x编写光敏电阻调节3.3伏大功率灯亮度(光强越大灯越暗,白天正常光强灯不亮,使用ADC,PWM等模块)望各位找一下错误或者提供一个可实现功能的代码
  • ¥20 verilog状态机方法流水灯
  • ¥15 pandas代码实现不了意图