【问题标题】:dismissing uiwebview关闭 uiwebview
【发布时间】:2011-03-05 06:37:01
【问题描述】:

好吧,我真的花了 2 天时间在这上面,这让我很难过。

从我的主 UIViewController 我调用了一个 WebViewController,它是一个 UIViewController,里面有一个 UIWebView:

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

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

在 UOLCategoriesWebViewController 内部,我调用了委托方法 shouldStartLoadWithRequest,当用户单击特定类型的链接时,它解析出参数并返回到主 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方法的函数中,我希望它调用委托方法并返回到主视图控制器。不幸的是,在执行了 goBackToMain 方法之后,它又回到了这个方法并继续返回 continueOrExit。

有没有办法让它真正退出而不返回任何东西?我试过多次退货无济于事。或者我可以在html页面中传递一些javascript来直接调用这个方法,这样我就不必通过这个委托方法了吗?

提前感谢您的帮助!

【问题讨论】:

  • 我很难理解您到底在做什么。如果 url 后缀是 categories_list.php 你希望它加载,如果不是你想做一些事情然后关闭?
  • 是的,完全正确:)
  • 不返回任何内容就退出是什么意思...我没有得到您的确切目的...
  • 我只希望当用户单击该类型的链接时,可以关闭 UIWebView 并从最初创建此 uiwebview 的 UIViewController 调用委托方法。

标签: iphone objective-c uiwebview


【解决方案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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多