Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ashwinr87

macrumors member
Original poster
Mar 9, 2011
81
0
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.
 
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:
https://forums.macrumors.com/threads/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.
 
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...

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:
https://forums.macrumors.com/threads/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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.