我陷入了一個奇怪的問題。我目前正在研究iPhone上的mapkit。我需要在我的地圖中顯示兩條路線,其中有一個來源城市和兩個不同的目的地。對於兩個城市之間的路線,我的代碼很好。爲此目的在我的代碼中的一個地方,我正在這樣做......。將多個字符串傳遞給stringByEvaluatingJavaScriptFromString的問題
- (void)loadWithStartPoint:(NSString *)startPoint endPoint:(NSString *)endPoint options:(UICGDirectionsOptions *)options {
[googleMapsAPI stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"loadDirections('%@', '%@', %@)", startPoint, endPoint, [options JSONRepresentation]]];
}
在上面的代碼中,stringByEvaluatingJavaScriptFromString將javascript傳遞給我的委託方法,因爲繪製了哪條路徑。 現在,我已經得出兩種不同的路線,爲此我在上面的代碼改變這樣的..
- (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。 但只有最後一個字符串被傳遞給委託方法。我錯過了什麼?請幫助我。自上週以來,我陷入了這個奇怪的問題。任何幫助,將不勝感激。 Thnx提前。
Thnx Ali爲我展示了NSThread&NSOperationQueue的清晰畫面。我會嘗試使用你建議的任何一種方式。 – iCoder4777