从UITableView传递URL时,UIWebView无法加载URL [英] UIWebView not loading URL when URL is passed from UITableView

查看:47
本文介绍了从UITableView传递URL时,UIWebView无法加载URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我要在我的应用程序中构建一个webView来显示我从UITableView中的选择传递的URL的内容.我知道UIWebView可以正确加载内容,因为如果您用硬编码说 http://www.google.ca 到NSURL,然后它会很好地加载,但是当我从UITableView传递从RSS反馈中解析的URL时,它将无法正确加载该URL.我尝试了调试器,然后在尝试对其进行解析之前,URL显示为nil,但是我可以使用NSLog将其值打印到控制台.

So i'm building a webView into my application to show the contents of a URL that I am passing from a selection in a UITableView. I know the UIWebView is loading content properly because if you hard code say http://www.google.ca into the NSURL then it loads fine, however when I'm passing the URL that I parsed from an RSS feed back from the UITableView it won't load the URL properly. I tried the debugger and the URL is coming out as nil right before I try and parse it, however I can use NSLog to print the value of it out to the console.

这是我的UIViewController中具有我的UIWebView的代码

here's the code in my UIViewController that has my UIWebView

 #import <UIKit/UIKit.h>


@interface ReadFeedWebViewController : UIViewController 
{
    NSString *urlToGet;
    IBOutlet UIWebView *webView;
}

@property(nonatomic, retain) IBOutlet UIWebView *webView;
@property(nonatomic, retain) NSString *urlToGet;

@end

这是我的实现的viewDidLoad方法的代码...

Here's the code for my implementation's viewDidLoad method...

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
    [super viewDidLoad];

    NSLog(@"Url inside Web View Controller - %@", urlToGet);
    NSURL *url = [NSURL URLWithString:urlToGet];

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [self.webView loadRequest:requestObj];
}

再一次,我可以将URL打印到NSLog,并且如果将URL硬编码到NSURL对象中,那么它将很好地加载到UIWebView中.这是我在UITableViewController中设置值的地方...

Once again, I can print the URL to NSLog fine and if I hard code the URL into the NSURL object then it will load fine in the UIWebView. Here is where I'm setting the value in my UITableViewController...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{   
    ReadFeedWebViewController *extendedView = [[ReadFeedWebViewController alloc] init];

    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    extendedView.urlToGet = [[stories objectAtIndex: storyIndex] objectForKey:@"link"];
    //NSLog([[stories objectAtIndex: storyIndex] objectForKey:@"summary"]);
    NSLog([[stories objectAtIndex: storyIndex] objectForKey:@"link"]);

    [self.navigationController pushViewController:extendedView animated:YES];

    [extendedView release];
}

但是,由于我可以在ExtendedView视图控制器中使用NSLog来打印值,因此我知道它已正确传递.干杯

However, since I can print the value using NSLog in the extendedView view controller I know it's being passed properly. Cheers

推荐答案

在viewDidLoad方法中,ReadToedWebViewController中的urlToGet为null,因为调用此方法时此变量尚未受影响.ViewController的初始化完成后,将调用viewDidLoad.我应该像这样在viewWillAppear方法中调用它:

The urlToGet is null into your ReadFeedWebViewController in the viewDidLoad method because when this method is call this variable is not yet affected. The viewDidLoad is call when the initialisation of the ViewController is finished. I should you to call it in the viewWillAppear method like that:

- (void)viewWillAppear:(BOOL)animated {
    NSURL *url = [NSURL URLWithString:urlToGet];

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [self.webView loadRequest:requestObj];
}

这篇关于从UITableView传递URL时,UIWebView无法加载URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆