从webViewDidStartLoad到webViewDidFinishLoad的变量设置为undefined

时间:2016-08-26 12:21:47

标签: ios uiwebview uiwebviewdelegate webviewdidfinishload

我有UIWebViewUIWebViewDelegate

在方法webViewDidStartLoad,我使用另一个类bs定义变量burningSoft

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"webViewDidStartLoad");
    NSLog(@"burningSoft 1: %@", self.context[@"burningSoft"]);
    [self initJS];
    NSLog(@"burningSoft 2: %@", self.context[@"burningSoft"]);
    [self setContext:[[self webView] valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]];
    if(self.context[@"burningSoft"].isUndefined) {
        self.context[@"burningSoft"] = [self bs];
    }
    NSLog(@"burningSoft 3: %@", self.context[@"burningSoft"]);
}

方法initJS看起来像

-(void)initJS
{
    NSLog(@"initJS");
    [self setBs:[[BurningSoft alloc] init]];
    [[self bs] setMain:self];
    if(self.context[@"burningSoft"].isUndefined) {
        self.context[@"burningSoft"] = [self bs];
    }
}

webViewDidFinishLoad看起来类似于webViewDidStartLoad

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"webViewDidFinishLoad");
    NSLog(@"burningSoft 4: %@", self.context[@"burningSoft"]);
    if(self.context[@"burningSoft"].isUndefined)
    {
        NSLog(@"UNDEFINED!!!");
        NSLog(@"[self bs] 1: %@", [self bs]);
        NSLog(@"burningSoft 5: %@", self.context[@"burningSoft"]);
        self.context[@"burningSoft"] = [self bs];
        NSLog(@"burningSoft 6: %@", self.context[@"burningSoft"]);
        NSLog(@"[self bs] 2: %@", [self bs]);
    }
    NSLog(@"burningSoft 7: %@", self.context[@"burningSoft"]);
}

所以我的第一页做了一些像这样的日志

2016-08-26 13:42:57.054 burningSoft[2325:342056] webViewDidStartLoad
2016-08-26 13:42:57.055 burningSoft[2325:342056] burningSoft 1: (null)
2016-08-26 13:42:57.055 burningSoft[2325:342056] initJS
2016-08-26 13:42:57.056 burningSoft[2325:342056] burningSoft 2: (null)
2016-08-26 13:42:57.060 burningSoft[2325:342056] burningSoft 3: < burningSoft: 0x7fc7a3616870>
2016-08-26 13:42:57.300 burningSoft[2325:342056] webViewDidFinishLoad
2016-08-26 13:42:57.300 burningSoft[2325:342056] burningSoft 4: < burningSoft: 0x7fc7a3616870>
2016-08-26 13:42:57.300 burningSoft[2325:342056] burningSoft 7: < burningSoft: 0x7fc7a3616870>

这是正确的。如果我输入了一些登录数据并且加载了另一个站点,我会得到像

这样的日志
2016-08-26 13:43:12.269 burningSoft[2325:342056] webViewDidStartLoad
2016-08-26 13:43:12.269 burningSoft[2325:342056] burningSoft 1: < burningSoft: 0x7fc7a3616870>
2016-08-26 13:43:12.270 burningSoft[2325:342056] initJS
2016-08-26 13:43:12.270 burningSoft[2325:342056] burningSoft 2: < burningSoft: 0x7fc7a3616870>
2016-08-26 13:43:12.270 burningSoft[2325:342056] burningSoft 3: < burningSoft: 0x7fc7a3616870>
2016-08-26 13:43:12.444 burningSoft[2325:342056] webViewDidFinishLoad
2016-08-26 13:43:12.445 burningSoft[2325:342056] burningSoft 4: undefined
2016-08-26 13:43:12.445 burningSoft[2325:342056] UNDEFINED!!!
2016-08-26 13:43:12.445 burningSoft[2325:342056] [self bs] 1: < burningSoft: 0x7fc7a5827230>
2016-08-26 13:43:12.445 burningSoft[2325:342056] burningSoft 5: undefined
2016-08-26 13:43:12.445 burningSoft[2325:342056] burningSoft 6: undefined
2016-08-26 13:43:12.445 burningSoft[2325:342056] [self bs] 2: < burningSoft: 0x7fc7a5827230>
2016-08-26 13:43:12.446 burningSoft[2325:342056] burningSoft 7: undefined

在日志2016-08-26 13:43:12.445 burningSoft[2325:342056] burningSoft 5: undefined之前一切正常。我的变量bs具有正确的内容,我将此方法中的内容设置为self.context[@"burningSoft"],但下一行中的日志告诉我没有任何内容。

有人知道为什么我的变量self.context[@"burningSoft"]在方法webViewDidStartLoadwebViewDidFinishLoad之间设置为未定义且不会设置?

0 个答案:

没有答案