【问题标题】:UIWebView only loads link after the second tryUIWebView 仅在第二次尝试后加载链接
【发布时间】:2014-12-11 20:02:05
【问题描述】:

我已经在这个问题上搜索了很长时间,但找不到解决我问题的答案。如果我遗漏了任何类似的问题并且有有效的解决方案,请发布指向它们的链接。

不管怎样……

我有一个充满按钮的菜单,单击这些按钮会打开一个 UIWebView,它会打开一个特定的网页。一切正常,但只有在第二次尝试之后。说得更清楚一点,我第一次点击按钮时,网页会卡住,但是当你关闭它并再次点击后,页面就可以正常加载了。

由于我使用了多个按钮,我决定将它们全部放在一个 IBOutlet 集合中:

@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *btnArray;

然后我设置了一个数组来保存集合中的所有按钮,为我们的 webview 类设置一个视图控制器(我们有一个单独的 webviews 类,这在过去没有给我造成任何问题)和webview 的导航控制器,最后我使用了一个for循环将所有按钮添加到数组中。

NSMutableArray *theButtons = [[NSMutableArray alloc] init];

webVC = [[WebviewViewController alloc] init];
webNav = [[UINavigationController alloc] initWithRootViewController:webVC];

for(int i = 0; i < [btnArray count]; i++)
{
    UIButton *btn = btnArray[i];
    btn.tag = i;
    [theButtons addObject:btn];
    [theButtons[i] addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}

我还设置了一个数组,其中包含我需要访问的所有页面的结束链接,它将附加到一个 url:

pages = @[@"exOne.php", @"exTwo.php", @"exThree.php", @"exFour.php", @"exFive.php", @"exSix.php"];

注意:以上所有代码都在-viewDidLoad中

这是处理单击按钮时发生的情况的方法:

-(void)buttonPressed : (id) sender
{
    UIButton *clicked = (UIButton *) sender;

    NSMutableString *url = [NSMutableString stringWithString:@"http://www.example.org/examples/"];


    for(int i = 0; i < [btnArray count]; i++)
    {
        if(i == clicked.tag)
        {
            [url appendString:pages[i]];
            [webVC loadURL:url];
            webNav.navigationBar.translucent = NO;
            webNav.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backPressed)];
            [self.navigationController pushViewController:webVC animated:YES];
        }
    }
}

如果从方法调用中不清楚

[webVC loadURL:url];

只需将链接发送到我们的 webviewViewController 类并加载链接。

关于为什么网页仅在第二次尝试后才加载的任何想法?

【问题讨论】:

    标签: ios uiwebview


    【解决方案1】:

    当您第一次调用 loadURL 时,您的 WebviewViewController 可能尚未加载。如果未加载视图控制器,则它的子视图和嵌入的 UIWebView 不可用。当您推动控制器时,它会被加载,并且第二次 loadURL 将起作用。

    【讨论】:

      猜你喜欢
      • 2011-05-27
      • 1970-01-01
      • 2022-08-15
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      相关资源
      最近更新 更多