Passing hexadecimal color values from Xcode to Javascript

Discussion in 'iOS Programming' started by ashwinr87, Jan 23, 2012.

  1. ashwinr87 macrumors member

    Mar 9, 2011

    I am trying to pass a Hexadecimal color value from objectiveC to Javascript. I am displaying a graph and I am trying to pass the color to javascript so that I dont need to hardcode it in my html page.

    Here is what I do -

    In my .m File,
    NSArray *colorArray = [NSArray arrayWithObjects:@"FFFFFF",@"000000", nil];
        colorString = [[colorArray valueForKey:@"description"] componentsJoinedByString:@","];
    In my webViewDidFinishLoad function,
    [graphView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"methodName1([%@])", colorString]];
    However, the javascript is not able to accept the values.

    In my Javascript function, what should I do to accept the values - FFFFFF and 000000 so that I can use them.

    I am trying to use the colors like this -
    line1.Set('chart.colors', ['FF0000', '000000']);
    where instead of giving the hexadecimal values there, I would like to get it from objC.

    It would be great if someone could help me out with this.
  2. chown33 macrumors 604

    Aug 9, 2009
    on the Western Slopes, with E. A. Poe
    What have you done to debug this?

    Post complete code. You haven't shown the JavaScript for methodname1.

    1. Describe what you expected to happen.
    2. Describe what actually happened.

    You have almost identical code in your earlier thread:

    Both have an array of strings being passed from Objective-C to JavaScript. So whatever you did to make that one work, try it on this one.
  3. ashwinr87 thread starter macrumors member

    Mar 9, 2011
    Here is what I did to get it working,

    NSArray *array1 = [NSArray arrayWithObjects:@"000000",@"8B0000", nil];
    colorString = [NSString stringWithFormat:@"'%@'", [array1 componentsJoinedByString:@"','"]];
    and when I pass this string to a javascript variable say "val1", var contains 2 elements - 000000 and 8B0000.
    Then here,
    line1.Set('chart.colors', [val1[0], val1[1]]);
    I got color to my graph...


Share This Page