重新加载 UIWebView

发布于 2024-11-18 04:21:19 字数 1204 浏览 2 评论 0原文

我在 viewDidLoad 中使用像这样的 UIWebView:

webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 88, 320, 400)]; //  init and create the UIWebView
    webView.autoresizesSubviews = YES;
    webView.userInteractionEnabled = NO;
    webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    [webView  setDelegate:self];//set the web view delegates for the web view to be itself
    NSURLRequest *requestObject = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com"]];//Create a URL object & Set the URL to go to for your UIWebView
    [webView loadRequest:requestObject];//load the URL into the web view.
    [self.view addSubview:webView];//add the web view to the content view
    [webView release], webView = nil;

当我尝试更改页面时,我尝试但是它不起作用:

[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];
[webView reload];

上面的代码是 TabBar 按钮的操作。

**编辑 : 当我发表评论时,

[webView release], webView = nil;

我可以看到苹果网站,但按下按钮时看不到 Facebook!屏幕在苹果网站上刷新,而不是在 Facebook 上……**

非常感谢您的帮助;-)

I'm using a UIWebView like that in the viewDidLoad:

webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 88, 320, 400)]; //  init and create the UIWebView
    webView.autoresizesSubviews = YES;
    webView.userInteractionEnabled = NO;
    webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    [webView  setDelegate:self];//set the web view delegates for the web view to be itself
    NSURLRequest *requestObject = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com"]];//Create a URL object & Set the URL to go to for your UIWebView
    [webView loadRequest:requestObject];//load the URL into the web view.
    [self.view addSubview:webView];//add the web view to the content view
    [webView release], webView = nil;

When I try to change the page, I try that BUT it doesn't working :

[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];
[webView reload];

The code that's just above is in the action of a TabBar button.

**EDIT :
When I comment

[webView release], webView = nil;

I can see the apple website but not facebook when I push the button! The screen is refreshing in the apple website and not facebook...**

Hundred of thanks to help ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北陌 2024-11-25 04:21:19

您已释放 webView 并将其设置为 nil。所以该对象不再存在。您正在调用 loadRequest 并在 nil 对象上重新加载,这不会执行任何操作。

假设您将 webView 作为类中的实例变量,请删除该行

[webView release], webView = nil;

并将其放入您的 dealloc 方法中,否则当您调用

[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];

时,您将在一个 nil 对象上调用它,这将不执行任何操作。

You've released and set your webView to nil. So the object no longer exists. You are calling loadRequest and reload on a nil object which will do nothing.

Assuming you have webView as an instance variable in your class, remove the line

[webView release], webView = nil;

and instead put that in your dealloc method otherwise when you call

[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];

then you will be calling it on a nil object which will do nothing.

空袭的梦i 2024-11-25 04:21:19

您在哪里尝试了这段代码?

[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];
[webView reload];

您已经发布了 webview 并将其设置为 Nil。因此,如果您在加载 www.apple.com 后执行此操作,它将无法工作。

[webView release], webView = nil;

将上面两行删除一次然后尝试。它应该有效。
希望这有帮助。

Where did you try this code ?

[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];
[webView reload];

You already released webview and make it Nil. So it will not work if you did it after it loads www.apple.com.

[webView release], webView = nil;

Remove above two lines once and then try. It should work.
Hope this help.

英雄似剑 2024-11-25 04:21:19

我看到的潜在问题是,您在将 webView 添加为子视图之后但在重新加载之前将其设置为 nil:

 [webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];
 [webView reload];

这里 webView 为 nil...该对象仍然存在(因为它是嵌入的)在视图中),但该变量不再指向它,因此消息将被忽略。

The potential problem I see is that you are setting the webView value to nil after adding it as subview, but before doing the reload:

 [webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];
 [webView reload];

here webView is nil... the object still exists (because it is embedded in a view), but the variable does not point to it anymore, so messages are simply ignored.

最笨的告白 2024-11-25 04:21:19

无需重新加载,

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
     [webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];
}

这可能会有所帮助..!!

no need of reload

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
     [webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.facebook.com"]]];
}

this may help..!!

℉絮湮 2024-11-25 04:21:19

阅读您的代码,我猜想您的 webView 在头文件中被声明为 @property 。加载 Apple 站点后释放此对象。我认为这就是它无法加载 Facebook 网站的原因。

[webView release]; 应仅放置在 - (void) dealloc 方法中。

如果有效请告诉我!

Reading your code, I guess that the your webView is declared as @property in the header file. You release this object after the Apple site is loaded. I think that's the reason why it doesn't load the facebook website.

The [webView release]; should be placed within the - (void) dealloc method only.

Please let me know if it works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文