为什么webview的Delegate方法没反应,代码如下

我在一个NSObject里做了一个WebView,然后用JS拿出一些node。但是,现在webview的Delegate方法没反应,代码如下

MyObject.h

@interface MyObject : NSObject

- (void)load;@end

MyObject.m

@interface MyObject ()<UIWebViewDelegate>@end@implementation MyObject// 这个方法没反应- (void)webViewDidStartLoad:(UIWebView *)webView
{    NSLog(@"start load");
}

- (id)init
{    self = [super init];    if (self) {        self.webView = [[UIWebView alloc] init];        self.webView.delegate = self;        return self;
    }    return nil;
}// 这个方法调用了- (void)load
{    NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://baidu.com"]];
    [self.webView loadRequest:requestObj];
}@end

调用是通过一个静态方法
Util.m

+ (void)getWebViewContent
{
    MyObject *obj = [[MyObject alloc] init];
    [obj load];
}


忽然笑
浏览 98回答 2
2回答

慕莱坞森

可能是你这个类没有实现UIWebViewDelegate哈

红颜莎娜

这个MyObject对象是一个局部的,在getWebViewContent函数内有效,函数执行结束,这个对象就没了。所以,在getWebViewContent中把MyObject做成静态就OK了+&nbsp;(void)getWebViewContent {&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;MyObject&nbsp;*obj&nbsp;=&nbsp;nil;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(nil&nbsp;==&nbsp;obj)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj&nbsp;=&nbsp;[[MyObject&nbsp;alloc]&nbsp;init]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;[obj&nbsp;load]; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS