Passing hexadecimal color values from Xcode to Javascript

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

  1. ashwinr87 macrumors member

    Joined:
    Mar 9, 2011
    #1
    Hi,

    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,
    Code:
    NSArray *colorArray = [NSArray arrayWithObjects:@"FFFFFF",@"000000", nil];
        
        colorString = [[colorArray valueForKey:@"description"] componentsJoinedByString:@","];
    In my webViewDidFinishLoad function,
    Code:
    [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 -
    Code:
    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

    Joined:
    Aug 9, 2009
    #2
    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:
    http://forums.macrumors.com/showthread.php?t=1309684

    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

    Joined:
    Mar 9, 2011
    #3
    Here is what I did to get it working,

    Code:
    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,
    Code:
    line1.Set('chart.colors', [val1[0], val1[1]]);
    I got color to my graph...

     

Share This Page