iphone - 除非点击按钮两次,否则 iOS 网页不会加载

标签 iphone html ios uiwebview nsstring

- (IBAction)saveButton:(id)sender
{
    NSURL *yourURL = [NSURL URLWithString: webpageURLLabel.text ];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:yourURL];
    if ([self checkURL] == YES) {
        [webpagePreview loadRequest:request];
        webpagePreview.scalesPageToFit = YES;
    }
}

- (BOOL)checkURL
{
    NSString *arrayOfStrings = [NSArray arrayWithObjects:@"http://", @"https://", nil];
    NSString *stringToSearchWithin = webpageURLLabel.text;

    BOOL found=NO;
    for (NSString *s in arrayOfStrings)
    {
        if ([stringToSearchWithin rangeOfString:s].location != NSNotFound)
        {
            found = YES;
            break;
        } else
        {
            webpageURLLabel.text = [NSString stringWithFormat:@"http://%@", webpageURLLabel.text];
            found = YES;
            break;
        }
    }
    return found;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    webpageTitleLabel.text = [webpagePreview stringByEvaluatingJavaScriptFromString:@"document.title"];
}

我调试了它,看起来应该可以加载。但出于某种原因,当您第一次点击按钮时,没有任何反应。

如果用户再次点击该按钮,它会正常工作。有什么建议吗?

最佳答案

在我看来,第一次单击按钮时,webpageURLLabel.text 可能没有有效的 URL,因此请求无效。然后,当 [self checkURL] 被调用时,webpageURLLabel.text 被设置为一个有效的 URL,因此下一次点击有效。

也许您应该在创建 NSURLRequest 之前调用 -checkURL

关于iphone - 除非点击按钮两次,否则 iOS 网页不会加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11704793/

相关文章:

html - 单选按钮可见性隐藏时所选图像周围的边框

html - 'a'( anchor 标记)的 CSS 样式不起作用

ios - iOS 发布版本的代码签名

iphone - ScrollView 仅在 View 出现第二次时才开始滚动

iphone - 我的 iPhone 视频只能以纵向模式播放

iphone - FBNativeDialogs 中缺少链接、名称、标题和描述

iphone - iOS 中的弱链接符号测试无法按预期工作

ios - 在 CollectionView 的 didSelectItemAtIndexPath 方法中获取 CGPoint

php - 如何在不破坏 CSS 标题的情况下在 Webfolio WordPress 主题中使用背景图片?

iphone - 创建 http 服务器并通过热点连接