【问题标题】:UIWebView Delegate Code Doesn't Seem RightUIWebView 委托代码似乎不正确
【发布时间】:2011-05-19 12:00:49
【问题描述】:

我已经使用我找到的 UIWebView 实现了示例代码,但它看起来不正确,尽管它可以工作。特别是因为它设置了 UIWevView 委托两次(在 viewDidLoad 和 viewWillAppear 中)。此外,myWebView 被设置为一个自动释放对象,但随后它在dealoc 中被释放。我希望有人能告诉我如何清理它。

// *** WebViewController.h ***

@interface WebViewController : UIViewController 
<UIWebViewDelegate>
{   
    UIWebView *myWebView;
    UIActivityIndicatorView *activityIndicator;
}

@property (nonatomic, retain) UIWebView *myWebView;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;

@end

// *** WebViewController.m ***

@synthesize myWebView;

- (void) viewDidLoad {
    [super viewDidLoad];

    // - - - - -> Create the UIWebView

    CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
    webFrame.origin.y += 42.0;
    webFrame.size.height -= 106.0;

    self.myWebView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];
    self.myWebView.backgroundColor = [UIColor whiteColor];
    self.myWebView.scalesPageToFit = YES;
    self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    self.myWebView.delegate = self;
    [self.view addSubview: self.myWebView];

    // - - - - -> Create the UIActivityIndicatorView

    self.activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
    [self.view addSubview: self.activityIndicator];
    self.activityIndicator.center = CGPointMake(135,438);

    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.someURL.com/"]]];

- (void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
   self.myWebView.delegate = self;
}

- (void) viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
    [self.myWebView stopLoading];   
    self.myWebView.delegate = nil;  
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void) dealloc {
    myWebView.delegate = nil;
    [myWebView release];
    [activityIndicator release];
    [super dealloc];
}

【问题讨论】:

    标签: xcode uiwebview uiwebviewdelegate


    【解决方案1】:

    这样做很好。这个想法很基本。您不希望代理人在离开屏幕时收到任何委托消息。所以设置代理-viewWillAppear:-viewWillDisappear:的思路是正确的。但我没有看到代码中实现了任何委托方法。

    【讨论】:

    • 但是代理也在-viewDidLoad中设置?我没有包括我正在使用的 UIWebView 委托方法(webViewDidStartLoad、webViewDidFinishLoad、didFailLoadWithError),只是为了避免帖子冗长。
    • 是的,你是对的。您可以安全地删除-viewDidLoad 中的委托分配。但是,它对对象的保留计数没有影响,因为委托被定义为 assign 属性。
    • webview 被释放两次(自动释放 + 释放),因为我们两次声明所有权。一次是在实例化对象时,一次是在设置 webView 属性时。我们在-viewDidLoad 中只放弃了一次所有权,并保留了对该对象的引用。这样我们就可以确保对象在我们需要的时候一直存在。
    • 不过如果你也可以这样做,直接myWebView = [[UIWebWebView alloc] initWithFrame:webFrame];
    • 当关闭这个视图时 myWebView retainCount = 2 after [myWebView release];所以我发现我仍然有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    相关资源
    最近更新 更多