2011-09-26 27 views
0

我陷入了一個奇怪的問題。我目前正在研究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提前。

回答

2

如阿里建議ü可以去WID .. 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]; 
} 

希望這將有助於你出來。

0

我想這是由於多線程不符合UIWebView。

您應該使用NSOperationQueue或GCD堆棧你讓他們在後臺異步地執行的stringByEvaluatingJavaScriptFromString電話,但仍然在主線程中執行它們(使用dispatch_get_main_queue()performSelectorOnMainThread:等)。

如果沒有關於多線程的實際問題,您也可以直接調用stringByEvaluatingJavaScriptFromString(爲什麼創建一個線程?即使您想單獨傳遞字符串,您仍可以多次調用該方法,是嗎?)

您也可以嘗試使用performSelector:withObject:afterDelay:(延遲爲0或0.01),以便在下一次迭代期間執行調用。通常,如果你真的不需要使用它們,請避免使用線程(有關Apple文檔中的詳細信息,請參閱「併發編程指南」和「線程編程指南」)。當它們存在時,首選使用異步方法,然後使用NSOperationQueues或GCD(並且只有在您沒有任何其他解決方案時,纔可以使用NSThreads)。這是因爲更高級的API會爲您處理棘手的問題,並且在處理多線程時讓問題變得不那麼複雜。

+0

Thnx Ali爲我展示了NSThread&NSOperationQueue的清晰畫面。我會嘗試使用你建議的任何一種方式。 – iCoder4777