【问题标题】:UIWebView not loading URLUIWebView 没有加载 URL
【发布时间】:2011-02-10 07:56:14
【问题描述】:

我无法让 UIWebView 加载我发送给它的 URL。在开始编写代码之前,我想知道 URL 是否必须以“http://”开头,还是可以以“www.”开头?

我正在使用 IBAction 将 UIView 推送到堆栈:

(IBAction)goToWebView {

WebViewController *webController = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]];

//set the strings
webController.webTitle = [self Title];

webController.webURL = [self website];

//Push the new view on the stack
[[self navigationController] pushViewController:webController animated:YES];
[webController release];
webController = nil;

}

然后这是我的 WebViewController.h 文件:

@interface WebViewController : UIViewController {

IBOutlet UIWebView *webView;
NSString *webTitle;
NSString *webURL;

}

@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) NSString *webTitle;
@property (nonatomic, retain) NSString *webURL; here

还有我的 WebViewController.m 文件:

- (void)viewDidLoad {
[super viewDidLoad];

NSString *urlAddress = webURL;

//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];

//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

//Load the request in the UIWebView.
[webView loadRequest:requestObj];

}

【问题讨论】:

  • 您可以与我们分享更多信息 - 您的应用程序崩溃了吗? webview 根本没有加载 URL 并保持空白吗?您还可以分享您尝试加载的网址吗?

标签: iphone objective-c uiviewcontroller uiwebview


【解决方案1】:

您的第一步应该是在 WebViewController 类的 viewDidLoad 代码中包含错误检查。至少有三个指针可能是nil,如果您在它们出现故障时抓住它们,就会让您深入了解页面加载代码可能出现的问题。

从那里,应该检查webURL 的内容(AFAIK 它应该以“http://”或类似开头)以及在viewDidLoad 末尾的webView 的正确实例化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    相关资源
    最近更新 更多