我遇到了 ARC 的问题。它不保留 webview。场景是我必须将 webview 从一个视图控制器发送到另一个视图控制器。原因是当用户搜索我想将他带到带有其他选项的新屏幕时。(我必须使用相同的 webview)
这是示例代码:我有一个 ViewController1,它有一个 webview(我在 xib 中添加了它。)我正在加载说 google,一旦用户搜索某些内容并且完成加载后,我必须将他带到一个新的视图控制器并在新的视图控制器中显示相同的 webview。
//ViewController1
@interface ViewController1 : UIViewController <UIWebViewDelegate>
@property (nonatomic, retain) UIWebView* testWebView;
@end
@implementation ViewController1
@synthesize testWebView;
- (void)viewDidLoad
{
[super viewDidLoad];
testWebView = [[UIWebView alloc]init];
testWebView.delegate = self;
[testWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com"]]];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *html = [testWebView stringByEvaluatingJavaScriptFromString:
@"document.body.innerHTML"];
if ([self.testWebView.request.url.absoluteString rangeOfString:@"output=search"].location != NSNotFound) {
ViewController2* newViewController = [[ViewController2 alloc] init];
[newViewController setTestWebView:self.testWebView];
[self.navigationController setViewControllers:[NSArray arrayWithObject:newViewController] animated:NO];
}
}
- (void)dealloc{
[self.testWebView stopLoading];
self.testWebView.delegate = nil;
self.testWebView = nil;
}
在第二个视图控制器中,我在延迟 10 秒后加载 stackoverflow.com。问题是它可以很好地加载stackoverflow,但它没有调用任何委托方法。为什么?
@interface ViewController2 : UIViewController <UIWebViewDelegate>
@property (nonatomic, retain) UIWebView* testWebView;
@end
@implementation ViewController2
@synthesize testWebView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.testWebView.delegate = self;
[self.view addSubview:testWebView];
[self performSelector:@selector(loadDifferentPage) withObject:nil afterDelay:10];
}
-(void)loadDifferentPage{
[self.testWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.stackoverflow.com/"]]];
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(@"%s", __PRETTY_FUNCTION__);
return YES;
}
ViewController2 保留了 webview,但没有调用委托方法。为什么?
谢谢赛