【问题标题】:uiwebview not loading request even with delegate = self即使使用委托=自我,uiwebview也不会加载请求
【发布时间】:2013-01-14 19:20:08
【问题描述】:

我创建了一个 NSObject 类并包含在内,在初始化中我创建了一个 uiwebview 将委托设置为 self 并发送加载请求。

由于某种原因,webViewDidFinishLoad 或 didFailLoadWithError 永远不会被解雇。我不知道为什么。

//
//  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 还是MRC?
  • 我正在使用 ARC 和 ios6 min.
  • 那就是问题所在。当您用完范围时,本地UIWebView 将立即被释放,因为没有任何东西可以使对象保持活动状态......但其他人也提到了明显的解决方案。

标签: ios objective-c xcode uiwebview uiwebviewdelegate


【解决方案1】:

如果您使用的是 ARC,那么问题在于您的 webView 变量是 init 方法的本地变量,因此在 Web 视图完成加载之前被释放。尝试将 Web 视图添加为实例变量:

@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 对象。

【讨论】:

    【解决方案2】:

    您忘记在头文件 (.h) 中添加此内容:

    #import <UIKit/UIWebView.h>
    

    【讨论】:

    • 使用方法:- (void)webViewDidStartLoad:(UIWebView *)webView 并在其中放一个NSLog(),看看它是否被记录,如果是,则意味着webView确实加载了。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多