重新加载 UIWebView
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您已释放 webView 并将其设置为 nil。所以该对象不再存在。您正在调用 loadRequest 并在 nil 对象上重新加载,这不会执行任何操作。
假设您将 webView 作为类中的实例变量,请删除该行
并将其放入您的 dealloc 方法中,否则当您调用
时,您将在一个 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
and instead put that in your dealloc method otherwise when you call
then you will be calling it on a nil object which will do nothing.
您在哪里尝试了这段代码?
您已经发布了 webview 并将其设置为 Nil。因此,如果您在加载 www.apple.com 后执行此操作,它将无法工作。
将上面两行删除一次然后尝试。它应该有效。
希望这有帮助。
Where did you try this code ?
You already released webview and make it Nil. So it will not work if you did it after it loads www.apple.com.
Remove above two lines once and then try. It should work.
Hope this help.
我看到的潜在问题是,您在将 webView 添加为子视图之后但在重新加载之前将其设置为 nil:
这里
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:
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.无需重新加载,
这可能会有所帮助..!!
no need of reload
this may help..!!
阅读您的代码,我猜想您的
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!