2010-06-18 78 views
1

好的,我真的花了2天時間,這讓我難住了。解僱uiwebview

從我的主要的UIViewController我叫WebViewController這是一個UIWebView裏面一個UIViewController:

UOLCategoriesWebViewController *ucwvcontroller = [[UOLCategoriesWebViewController alloc] initWithNibName:@"UOLCategoriesWebViewController" bundle:nil]; 

    [self presentModalViewController:ucwvcontroller animated:YES]; 
    [ucwvcontroller release]; 

的UOLCategoriesWebViewController裏面我已經打電話委託方法shouldStartLoadWithRequest當用戶點擊一個特定類型的鏈接,其中它解析出PARAMS並返回到主的UIViewController(或者至少,這就是我想要它做):

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { 
    BOOL continueOrExit = YES; 
    NSURL *url = request.URL; 
    NSString *urlString = [url relativeString]; 
    NSString *urlParams = [url query]; 
    NSArray *urlParamArr = [urlParams componentsSeparatedByString:@"&"]; 
    NSLog(@"the url is: %@",urlString); 
    //NSLog(@"the params are: %@,%@",[urlParamArr objectAtIndex:0],[urlParamArr objectAtIndex:1]); 

    //BOOL endTrain1 = [[urlParamArr objectAtIndex:1] hasPrefix:@"subCatValue"]; 
    //BOOL endTrain2 = ([urlParamArr objectAtIndex:1 
    //NSLog(@"Number of elements: %@",[urlParamArr count]); 
    if (navigationType == UIWebViewNavigationTypeLinkClicked) 
    { 
     //NSLog(@"Enter into His glory"); 

     if ([urlString hasSuffix:@"categories_list.php"]) { 

      //NSLog(@"2nd Heaven"); 
      continueOrExit = YES; 

     }else if ([[urlParamArr objectAtIndex:1] hasPrefix:@"subCatValue"]) { 
      continueOrExit = NO; 
      NSLog(@"end of the train"); 
      NSArray *firstParamStringArr = [[urlParamArr objectAtIndex:0] componentsSeparatedByString:@"="]; 
      NSArray *secondParamStringArr = [[urlParamArr objectAtIndex:1] componentsSeparatedByString:@"="]; 
      NSString *catSelected = [firstParamStringArr objectAtIndex:1]; 
      NSString *subCatSelected = [secondParamStringArr objectAtIndex:1]; 



      //go back to native app 
      [self goBackToMain:catSelected andSubCat:subCatSelected]; 



     } 
    } 
    return continueOrExit; 
} 

其實在上面的函數在那裏我打電話了goBackToMain方法我希望它調用委託方法並返回到mainviewcontroller。不幸的是,在執行goBackToMain方法後,它返回到此方法並繼續返回continueOrExit。

無論如何要真正退出而不返回任何東西嗎?我試圖把多個回報無濟於事。或者我可以在HTML頁面傳遞一些JavaScript來直接調用這個方法,這樣我就不必經歷這個委託方法了?

感謝您的幫助!

+0

我有點麻煩理解你到底在做什麼。如果url後綴是你想加載的categories_list.php,如果不是你想做一些東西然後解僱? – Rudiger 2010-06-18 05:50:31

+0

是的,這是完全正確的:) – chimgrrl 2010-06-18 06:11:55

+0

你是什麼意思退出沒有返回任何東西...我沒有得到你的確切目的... – 2010-06-18 06:31:42

回答

1

你可以嘗試的是在委託方法執行後推遲goBackToMain:andSubCat:的調用。這樣,代理方法可以返回並調用然後處理:

NSArray *firstParamStringArr = [[urlParamArr objectAtIndex:0] componentsSeparatedByString:@"="]; 
NSArray *secondParamStringArr = [[urlParamArr objectAtIndex:1] componentsSeparatedByString:@"="]; 
NSString *catSelected = [firstParamStringArr objectAtIndex:1]; 
NSString *subCatSelected = [secondParamStringArr objectAtIndex:1]; 

NSMethodSignature *sig = [self methodSignatureForSelector:@selector(goBackToMain:andSubCat:)]; 
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig]; 
[inv retainArguments]; 
[inv setTarget:self]; 
[inv setSelector:selector]; 
[inv setArgument:&catSelected atIndex:2]; 
[inv setArgument:&subCatSelected atIndex:3]; 
[inv performSelector:@selector(invoke) withObject:nil afterDelay:0.0]; 
+0

謝謝!這給了我一個良好的開始正確的方向:) – chimgrrl 2010-06-18 09:08:52