我在一个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];
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
感谢@freeface 提醒,找到问题了。这个MyObject对象是一个局部的,在getWebViewContent函数内有效,函数执行结束,这个对象就没了。
所以,在getWebViewContent中把MyObject做成静态就OK了
+ (void)getWebViewContent { static MyObject *obj = nil; if (nil == obj) { obj = [[MyObject alloc] init]; } [obj load]; }这个类的其他代码呢?
可能是你这个类有没有实现UIWebViewDelegate