javascript - 将多个字符串传递给 stringByEvaluatingJavaScriptFromString 时出现问题

标签 javascript iphone uiwebview nsthread

我遇到了一个奇怪的问题。我目前正在 iPhone 上开发 Mapkit。我需要在 map 中显示两条路线,其中有一个源城市和两个不同的目的地。对于两个城市之间的路线,我的代码很好。为此,我在代码中的一个地方这样做了......

- (void)loadWithStartPoint:(NSString *)startPoint endPoint:(NSString *)endPoint options:(UICGDirectionsOptions *)options {
[googleMapsAPI stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"loadDirections('%@', '%@', %@)", startPoint, endPoint, [options JSONRepresentation]]];
}

在上面的代码中,stringByEvaluatingJavaScriptFromString 由于绘制了路线而将 javascript 传递给我的委托(delegate)方法。 现在,我必须绘制两条不同的路线,为此我已经更改了上面的代码,如下所示..

- (void)loadWithStartPoint:(NSString *)startPoint endPoint:(NSMutableArray *)endPoints options:(UICGDirectionsOptions *)options {
    for (int idx = 0; idx < [endPoints count];idx ++) 
    {
        NSString* msg = [NSString stringWithFormat:@"loadDirections('%@', '%@', %@)", startPoint, [endPoints objectAtIndex:idx], [options JSONRepresentation]];
        mstr = [msg retain];
        if (idx == 0)
        {
            [googleMapsAPI stringByEvaluatingJavaScriptFromString:msg];
        }
        else {
            [NSThread detachNewThreadSelector:@selector(loadroute:) toTarget:self withObject:mstr];
        }
    }
}

我有以下内容来创建和实现 NSThread。

-(void)loadroute :(NSString *)message
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    [self performSelectorOnMainThread:@selector(loadComplete:) withObject:message waitUntilDone:YES];
    [pool release];

}

-(void)loadComplete:(NSString *)message
{
    [googleMapsAPI stringByEvaluatingJavaScriptFromString:message];
}

在这里,我创建了另一个线程,因此我可以单独将字符串传递给 stringByEvaluatingJavaScriptFromString。 但只有最后一个字符串被传递给委托(delegate)方法。我缺少什么?请帮帮我。从上周开始我就陷入了这个奇怪的问题。任何帮助,将不胜感激。提前致谢。

最佳答案

按照阿里的建议,你可以去..performSelector:withObject:afterDelay:它会给你想要的结果.. 你可以像这样编写你的代码..

 - (void)loadWithStartPoint:(NSString *)startPoint endPoint:(NSMutableArray *)endPoints options:(UICGDirectionsOptions *)options {
            for (int idx = 0; idx < [endPoints count];idx ++) 
            {
    NSString* msg = [NSString stringWithFormat:@"loadDirections('%@', '%@', %@)", startPoint, [endPoints objectAtIndex:idx], [options JSONRepresentation]];
            mstr = [msg retain];

             [self performSelector:@selector(loadComplete:) withObject:nil afterDelay:0.5];
            }
        }

-(void)loadComplete:(NSString *)message
 {
        [googleMapsAPI stringByEvaluatingJavaScriptFromString:message];
 }

希望这对你有帮助。

关于javascript - 将多个字符串传递给 stringByEvaluatingJavaScriptFromString 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7554379/

相关文章:

iphone - 验证可用的网络连接和启用定位服务以使用应用程序

iphone - iOS 上键盘动画所需的时间

html - 如何在 UIWebView 中显示本地 html 文件?

iphone - 在iPhone上使用UIWebView禁用全屏youtube视频显示

iphone - 在 UIWebView 中执行操作后隐藏文本选择句柄

javascript - 无法从 React 中的 onClick 调用函数

javascript - 如何将 GET api 写入 Node.js 中的现有文件中

ios - 在 ViewController 之间传递 NSString 返回 NULL

php - 哪一端创建 session /cookie?

javascript - 在MouseEnter上动态添加/修改className