0

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 Answers 2

2

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];
}
Sign up to request clarification or add additional context in comments.

Comments

0

here is a brief explanation on how to create a function in objective C :

- (void) exampleFunction: (int)x y:(int)y
{
do some stuff HERE, changing BG etc
}

you can call this function by :

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

this is the basic way of creating a function and using it, there are other more complex things that you might want to read about.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.