uiwebview 不加载请求,即使委托 = self [英] uiwebview not loading request even with delegate = self
问题描述
我创建了一个 NSObject 类并包含在初始化中,我创建了一个 uiwebview 将委托设置为 self 并发送加载请求.
I have created a NSObject class and included , in the init i create a uiwebview set the delegate to self and send the load request.
出于某种原因,webViewDidFinishLoad 或 didFailLoadWithError 永远不会被触发.我不明白为什么.
For some reason webViewDidFinishLoad or didFailLoadWithError never get fired. I can't figure why.
//
// RXBTest.h
#import <Foundation/Foundation.h>
@interface RXBTest : NSObject <UIWebViewDelegate>
@end
<小时>
// RXBTest.m
// pageTest
#import "RXBTest.h"
@implementation RXBTest
- (id) init
{
if((self=[super init])){
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
[webView setDelegate:self];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]]];
}
return self;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"ERROR LOADING WEBPAGE: %@", error);
}
- (void) webViewDidFinishLoad:(UIWebView*)webView
{
NSLog(@"finished");
}
@end
有人有什么想法吗?
谢谢鲁迪
推荐答案
如果您使用的是 ARC,那么问题在于您的 webView
变量是 init
的本地变量方法,因此在 web 视图完成加载之前被释放.尝试将 Web 视图添加为实例变量:
If you are using ARC, then the problem is that your webView
variable is local to the init
method and therefore is getting deallocated before the web view finishes loading. Try adding the web view as an instance variable:
@interface RXBTest : NSObject <UIWebViewDelegate>
{
UIWebView* webView;
}
@end
@implementation RXBTest
- (id) init
{
if((self=[super init])){
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
[webView setDelegate:self];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]]];
}
return self;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"ERROR LOADING WEBPAGE: %@", error);
}
- (void) webViewDidFinishLoad:(UIWebView*)webView
{
NSLog(@"finished");
}
@end
如果你没有使用 ARC,你需要记得在 dealloc 方法中释放你的 webView
对象.
If you are not using ARC, you will need to remember to release your webView
object in the dealloc method as well.
这篇关于uiwebview 不加载请求,即使委托 = self的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!