【问题标题】:shouldStartLoadWithRequest delegate method never calledshouldStartLoadWithRequest 委托方法从未被调用
【发布时间】:2013-07-02 20:37:09
【问题描述】:

我正在创建一个具有 web 视图的应用程序。当用户单击 webview 中的链接或按钮时,我希望能够获取新的 url,并编辑新的 url。

shouldStartLoadWithRequest 应该可以解决问题,但是当我单击链接时永远不会调用该方法。我找不到为什么这不起作用。

我在某处读到需要添加这一行:

webView.delegate = (id)self;

我试过了,还是一样的问题。请帮忙

AppDelegate.m

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *currentURL = [[request URL] absoluteString] ;

    NSLog(@"Url: %@", currentURL);
    return YES;
}

控制器.m

- (void)viewDidLoad
{
    webView.delegate = (id)self;
    NSString *tokenString = @"123";
    [super viewDidLoad];

    NSString *fullURL = [[NSString alloc] initWithFormat:@"http://192.168.1.69:8888/webapp/test.php?uid=%@", tokenString];
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
}

【问题讨论】:

    标签: iphone ios xcode uiwebview appdelegate


    【解决方案1】:

    如果您的Controller 实例将自己添加为委托,那么您添加到AppDelegate 实例的方法将永远不会被调用。

    shouldStartLoadWithRequest 方法移动到您的Controller 类中。

    【讨论】:

      【解决方案2】:

      您已将视图控制器设置为 Web 视图的委托。但是你已经在我们的应用程序的委托中实现了 UIWebView 委托方法。

      尝试在您的视图控制器中实现该委托方法。您的视图控制器是您的网络视图的代表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多